Swift 4.1 deinitialize和deallocate(capacity :)已弃用

问题描述 投票:4回答:1

我一直在说这个形成CGPoint的C数组:

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 beta中的Swift 4.1)deinitializedeallocate(capacity:)都被弃用了。看起来我现在应该说的可能是:

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

是对的吗?

swift unsafe-pointers swift4.1
1个回答
6
投票

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

特别是:

从deallocate(capacity :)中删除容量将结束对deallocate()的干扰,显而易见deallocate()将释放整个内存块,就像在其上调用free()一样。

旧的deallocate(capacity :)方法应该被标记为已弃用并最终被删除,因为它当前鼓励使用危险的错误代码。

© www.soinside.com 2019 - 2024. All rights reserved.