CoreFounction是開(kāi)源的,需要了解的可以點(diǎn)擊這里下載整個(gè)CoreFountion源碼,我這里作為學(xué)習(xí)筆記,如有不對(duì)請(qǐng)指正。
1.CFStringRef和CFMutableStringRef
typedef const struct CF_BRIDGED_TYPE(NSString) __CFString * CFStringRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableString) __CFString * CFMutableStringRef;
CFStringRef本質(zhì)就是一個(gè)結(jié)構(gòu)體
__CFString的源碼如下
struct __CFString {
CFRuntimeBase base;
union { // In many cases the allocated structs are smaller than these
struct __inline1 {
CFIndex length;
} inline1; // Bytes follow the length
struct __notInlineImmutable1 {
void *buffer; // Note that the buffer is in the same place for all non-inline variants of CFString
CFIndex length;
CFAllocatorRef contentsDeallocator; // Optional; just the dealloc func is used
} notInlineImmutable1; // This is the usual not-inline immutable CFString
struct __notInlineImmutable2 {
void *buffer;
CFAllocatorRef contentsDeallocator; // Optional; just the dealloc func is used
} notInlineImmutable2; // This is the not-inline immutable CFString when length is stored with the contents (first byte)
struct __notInlineMutable notInlineMutable;
} variants;
};
CFString有多種創(chuàng)建方法,大體上分為兩種,利用CFStringCreateWith...函數(shù)或者利用CFSTR()這個(gè)macro
CFStringRef stringRef = CFSTR("string");
CFStringRef stringRef = CFStringCreateWithCString(kCFAllocatorDefault, "string", kCFStringEncodingUTF8);
區(qū)別就在于利用函數(shù)生成的string在堆區(qū)(打印地址感覺(jué)像在棧區(qū)。。),使用完需要調(diào)用CFRelease()釋放相關(guān)內(nèi)存,用macro生成的字符串放在常量區(qū),此時(shí)跟用@""生成的string地址相同,不要關(guān)心其內(nèi)存問(wèn)題,你要真對(duì)其release也無(wú)所謂。
蘋(píng)果官方文檔介紹如下:
A value returned by CFSTR has the following semantics:
Values returned from CFSTR are not released by CFString—they are guaranteed to be valid until the program terminates.
You can retain and release values returned from CFSTR in a balanced fashion, like any other CFString, but you are not required to do so.
帶CF_BRIDGED_TYPE這個(gè)東西的都可以通過(guò)__bridge和NS相關(guān)對(duì)象相互轉(zhuǎn)換
CFStringRef和NSString相關(guān)轉(zhuǎn)換如下
NSString *yourNSString = (__bridge NSString *)yourCFString;
CFStringRef yourCFString = (__bridge CFStringRef)yourNSString;
插播一下bridge相關(guān)的東西
__bridge又分為_(kāi)_bridge,__bridge_transfer和__bridge_retained
__bridge就是簡(jiǎn)單的做類型轉(zhuǎn)換(內(nèi)存地址不會(huì)變),只負(fù)責(zé) CF 到 OC 之間的對(duì)象類型轉(zhuǎn)換,并沒(méi)有把內(nèi)存管理的權(quán)限轉(zhuǎn)交,本著誰(shuí)創(chuàng)建誰(shuí)負(fù)責(zé)的原則,誰(shuí)創(chuàng)建內(nèi)存由誰(shuí)負(fù)責(zé)。
__bridge_retained和CFBridgingRetain(<#id _Nullable X#>)相同,用于OC對(duì)象轉(zhuǎn)換到CF對(duì)象(什么對(duì)象不對(duì)象的,本質(zhì)都是結(jié)構(gòu)體),并將其內(nèi)存管理權(quán)限轉(zhuǎn)移,由CF管理其內(nèi)存,如果是NSString可以不用關(guān)心其內(nèi)存,但是其他OC對(duì)象就不行了,需要使用CFRelease()進(jìn)行內(nèi)存管理。
__bridge_transfer和CFBridgingRelease(<#CFTypeRef _Nullable X#>)相同,用于CF轉(zhuǎn)換為OC對(duì)象,并將內(nèi)存管理權(quán)限交給OC,ARC下就需要關(guān)心內(nèi)存相關(guān)的東西。
2. CFDictionaryRef和CFMutableDictionaryRef
typedef const struct CF_BRIDGED_TYPE(NSDictionary) __CFDictionary * CFDictionaryRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableDictionary) __CFDictionary * CFMutableDictionaryRef;
其構(gòu)造函數(shù)不多
CFDictionaryCreate(<#CFAllocatorRef allocator#>, <#const void **keys#>, <#const void **values#>, <#CFIndex numValues#>, <#const CFDictionaryKeyCallBacks *keyCallBacks#>, <#const CFDictionaryValueCallBacks *valueCallBacks#>)
CFAllocatorRef:分配內(nèi)存用使用kCFAllocatorDefault或者NULL即可使用當(dāng)前默認(rèn)的分配器
keys:存放key的c數(shù)組
values:存放value的c數(shù)組
numValues:dic的鍵值對(duì)數(shù)
keyCallBacks:鍵的回調(diào)
valueCallBacks:值得回調(diào)
Forexample:
CFStringRef keys[] = {CFSTR("one"), CFSTR("two"), CFSTR("three")};
CFStringRef values[] = {CFSTR("red"), CFSTR("yellow"), CFSTR("blue")};
CFDictionaryRef dicRef = CFDictionaryCreate(kCFAllocatorDefault, (void *)keys, (void *)values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef添加數(shù)據(jù)有兩種方法
CF_EXPORT
void CFDictionarySetValue(CFMutableDictionaryRef theDict, const void *key, const void *value);
如果key存在,則用新的value替換
void CFDictionaryAddValue(CFMutableDictionaryRef theDict, const void *key, const void *value);
如果key存在,則不做其他操作。
3.CFArrayRef和CFMutableArrayRef
typedef const struct CF_BRIDGED_TYPE(NSArray) __CFArray * CFArrayRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableArray) __CFArray * CFMutableArrayRef;
struct __CFArray {
CFRuntimeBase _base;
CFIndex _count; /* number of objects */
CFIndex _mutations;
int32_t _mutInProgress;
__strong void *_store; /* can be NULL when MutableDeque */
};
Forexample:
CFStringRef strings[3] = { CFSTR("HOW") ,CFSTR("ARE"),CFSTR("YOU")};
CFArrayRef array = CFArrayCreate(NULL, (void*)strings, 3, &kCFTypeArrayCallBacks);
CFMutableArrayRef mutableArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
4.CFRunLoopTimerRef
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSTimer) __CFRunLoopTimer * CFRunLoopTimerRef;
同樣的 CFRunLoopTimerRef也可以通過(guò)bridge直接轉(zhuǎn)換
CF_EXPORT CFRunLoopTimerRef CFRunLoopTimerCreate(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, CFRunLoopTimerCallBack callout, CFRunLoopTimerContext *context);
CF_EXPORT CFRunLoopTimerRef CFRunLoopTimerCreateWithHandler(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, void (^block) (CFRunLoopTimerRef timer)) API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));