Using CoreFoundation in Swift painlessly.
import SwiftCF
let str: Any = "foo" as CFString
let s0 = str as? CFString
// ❗️ Conditional downcast to CoreFoundation type 'CFString' will always succeed
let s1: CFString = cfCast(str) // or
let s2 = cfCast(str, to: CFString.self) // or
let s3 = CFString.cast(str)
// ✅
let cfStr: CFString = .from("foo")
let nsStr: NSString = cfStr.asNS
let str: NSString = cfStr.asSwift
let str = "foo" as CFString
str.length
// Replace CFStringGetLength(str)
CFStringTokenizer.create(string: str)
// Replace 😱 CFStringTokenizerCreate(kCFAllocatorDefault, str, CFRange(location: 0, length: CFStringGetLength(str)), kCFStringTokenizerUnitWord, CFLocaleGetSystem())
let arr = [1, 3.14, "foo"] as CFArray
for item in arr {
// 😃 CFArray now conforms to RandomAccessCollection.
}
- Swift 5.0+