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
推荐阅读
- Swift中willSet和didSet的简述
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- LeetCode算法题-11.|LeetCode算法题-11. 盛最多水的容器(Swift)
- 性能测试中QPS和TPS的区别
- iOS-Swift-map|iOS-Swift-map filter reduce、函数式编程
- Swift|Swift ----viewController 中addChildViewController
- SwiftUI|SwiftUI iOS 瀑布流组件之仿CollectionView不规则图文混合(教程含源码)
- Swift高级应用|Swift高级应用 -01
- 浅谈教育与医学之本质和医学生培养之三观
- 浅谈朋友圈