Swift 4.1 deinitialize and deallocate(capacity:) 弃用

Swift 4.1 deinitialize and deallocate(capacity:) deprecated

我一直这样说是为了形成一个CGPoint数组:

let arr = UnsafeMutablePointer<CGPoint>.allocate(capacity:4)
defer {
    arr.deinitialize()
    arr.deallocate(capacity:4)
}
arr[0] = CGPoint(x:0,y:0)
arr[1] = CGPoint(x:50,y:50)
arr[2] = CGPoint(x:50,y:50)
arr[3] = CGPoint(x:0,y:100)

现在(Xcode 9.3 测试版中的 Swift 4.1)deinitializedeallocate(capacity:) 都已弃用。看来我现在应该说的可能是:

defer {
    arr.deinitialize(count:4)
    arr.deallocate()
}

对吗?

是的,那是 SE-0184 Unsafe[Mutable][Raw][Buffer]Pointer: add missing methods, adjust existing labels for clarity, and remove deallocation size 的一部分, 已在 Swift 4.1.

中实现

特别是:

Removing capacity from deallocate(capacity:) will end the confusion over what deallocate() does, making it obvious that deallocate() will free the entire memory block at self, just as if free() were called on it.

The old deallocate(capacity:) method should be marked as deprecated and eventually removed since it currently encourages dangerously incorrect code.