CFDictionary|CFDictionary In Swift

** # 浅谈CFDictionary In Swift **
Access low-level functions, primitive data types, and various collection types that are bridged seamlessly with the Foundation framework.
意思是他是一套底层的API,拥有原始的数据类型,各种与Foundation无缝结合的全集
首先来回顾一下OC中使用CFDictionary的基本用法

CFMutableDictionaryRef dict = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, NULL, NULL) ; NSString *nameKey = @"name" ; NSString *name = @"007" ; CFDictionaryAddValue(dict, (__bridge void *)nameKey, (__bridge void *)name) ; const void *result = CFDictionaryGetValue(dict, (__bridge void *)nameKey) ; NSLog(@"姓名:%@",(__bridge NSString *)result) ; CFRelease(dict) ;

轻车熟路,很简单基本的一个用法,那么在swift中又是怎么使用的呢?
let dict = CFDictionaryCreateMutable(defaultAllocator, 0, nil, nil) let key = Unmanaged.passRetained("name" as NSString).autorelease().toOpaque() let value = https://www.it610.com/article/Unmanaged.passRetained("007" as NSString).autorelease().toOpaque() CFDictionaryAddValue(dict, key, value) if let p = CFDictionaryGetValue(dict, key) { let result = Unmanaged.fromOpaque(p).takeUnretainedValue() print(result) }

【CFDictionary|CFDictionary In Swift】以上就是CFDictionary在OC与Swift中的差别
基本上Core Foundation中出现的UnsafeRawPointer你都可以看成它是Unmanged的指针,通过toOpaque()与fromOpaque()来转换,从而从其中得到实际存储的值

    推荐阅读