CoreFounction是开源的,需要了解的可以点击这里下载整个CoreFountion源码,我这里作为学习笔记,如有不对请指正。
1.CFStringRef和CFMutableStringRef
typedef const struct CF_BRIDGED_TYPE(NSString) __CFString * CFStringRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableString) __CFString * CFMutableStringRef;
CFStringRef本质就是一个结构体
__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有多种创建方法,大体上分为两种,利用CFStringCreateWith...函数或者利用CFSTR()这个macro
CFStringRef stringRef = CFSTR("string");
CFStringRef stringRef = CFStringCreateWithCString(kCFAllocatorDefault, "string", kCFStringEncodingUTF8);
区别就在于利用函数生成的string在堆区(打印地址感觉像在栈区。。),使用完需要调用CFRelease()释放相关内存,用macro生成的字符串放在常量区,此时跟用@""生成的string地址相同,不要关心其内存问题,你要真对其release也无所谓。
苹果官方文档介绍如下:
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这个东西的都可以通过__bridge和NS相关对象相互转换
CFStringRef和NSString相关转换如下
NSString *yourNSString = (__bridge NSString *)yourCFString;
CFStringRef yourCFString = (__bridge CFStringRef)yourNSString;
插播一下bridge相关的东西
__bridge又分为__bridge,__bridge_transfer和__bridge_retained
__bridge就是简单的做类型转换(内存地址不会变),只负责 CF 到 OC 之间的对象类型转换,并没有把内存管理的权限转交,本着谁创建谁负责的原则,谁创建内存由谁负责。
__bridge_retained和CFBridgingRetain(<#id _Nullable X#>)相同,用于OC对象转换到CF对象(什么对象不对象的,本质都是结构体),并将其内存管理权限转移,由CF管理其内存,如果是NSString可以不用关心其内存,但是其他OC对象就不行了,需要使用CFRelease()进行内存管理。
__bridge_transfer和CFBridgingRelease(<#CFTypeRef _Nullable X#>)相同,用于CF转换为OC对象,并将内存管理权限交给OC,ARC下就需要关心内存相关的东西。
2. CFDictionaryRef和CFMutableDictionaryRef
typedef const struct CF_BRIDGED_TYPE(NSDictionary) __CFDictionary * CFDictionaryRef;
typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableDictionary) __CFDictionary * CFMutableDictionaryRef;
其构造函数不多
CFDictionaryCreate(<#CFAllocatorRef allocator#>, <#const void **keys#>, <#const void **values#>, <#CFIndex numValues#>, <#const CFDictionaryKeyCallBacks *keyCallBacks#>, <#const CFDictionaryValueCallBacks *valueCallBacks#>)
CFAllocatorRef:分配内存用使用kCFAllocatorDefault或者NULL即可使用当前默认的分配器
keys:存放key的c数组
values:存放value的c数组
numValues:dic的键值对数
keyCallBacks:键的回调
valueCallBacks:值得回调
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添加数据有两种方法
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也可以通过bridge直接转换
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));