2

我想从一个数组创建一个 CFArray

以前在 Swift 2 中,我能够做到这一点:

let array = [0, 1, 2, 3]
let arrayPointer = UnsafeMutablePointer<UnsafePointer<Void>>(array)
let cfArray = CFArrayCreate(kCFAllocatorDefault, arrayPointer, array.count, nil)

现在在 Swift 3 CFArrayCreate 中期望UnsafeMutablePointer<UnsafeRawPointer?>!作为第二个参数

我尝试这样做的一种方法是:

let array: [Int] = [0, 1, 2, 3]
var rawPointer: UnsafeRawPointer? = UnsafeRawPointer(array)
let cfArray = withUnsafeMutablePointer(to: &rawPointer) { ppArray in
    CFArrayCreate(kCFAllocatorDefault, ppArray, array.count, nil)
}

然而,这将创建 CFArray 元素为 [rawPointer, 垃圾, 垃圾, 垃圾的值] 而不是 [0, 1, 2, 3]

我将如何将这个UnsafeMutablePointer<UnsafeRawPointer?>!参数从数组中取出?

4

1 回答 1

3

你可以做

let cfArray = array as CFArray
于 2016-09-29T03:54:41.873 回答