gpt4 book ai didi

Swift 4.1 取消初始化和解除分配(容量 :) deprecated

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:57 25 4
gpt4 key购买 nike

我一直在这样说来形成 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()
}

是这样吗?

最佳答案

是的,那是 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.

关于Swift 4.1 取消初始化和解除分配(容量 :) deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48570240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com