我一直这样说来形成 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)deinitialize
and deallocate(capacity:)
已弃用。看来我现在应该说的是:
defer {
arr.deinitialize(count:4)
arr.deallocate()
}
是对的吗?
是的,这是一部分SE-0184 不安全[可变][原始][缓冲区]指针:添加缺少的方法,调整现有标签以确保清晰度,并删除释放大小 https://github.com/apple/swift-evolution/blob/master/proposals/0184-unsafe-pointers-add-missing.md,
已在 Swift 4.1 中实现。
尤其:
从 deallocate(capacity:) 中删除容量将结束对 deallocate() 功能的混淆,很明显,deallocate() 将自行释放整个内存块,就像在其上调用 free() 一样。
旧的 deallocate(capacity:) 方法应标记为已弃用并最终删除,因为它目前鼓励危险的错误代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)