在 Swift 3.1 中,UnsafeMutablePointer.initialize(from:) 已弃用
In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated
在 Swift 3.1 中,UnsafeMutablePointer.initialize(from:) 已弃用。 Xcode 建议我改用 UnsafeMutableBufferPointer.initialize(from:)。我有一个看起来像这样的代码块:
let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
pointer.initialize(from: repeatElement(0, count: 64))
由于弃用,代码给了我一个编译时警告。所以我要把它改成:
let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
let buffer = UnsafeMutableBufferPointer(start: pointer, count: 64)
_ = buffer.initialize(from: repeatElement(0, count: 64))
这样做正确吗?我只是想确保我做的正确。
这是正确的,但是您可以使用
稍微简单地分配和初始化内存
let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
pointer.initialize(to: 0, count: 64)
创建一个缓冲区指针视图仍然有用,因为
是一个集合,有一个count
属性 并且可以枚举:
let buffer = UnsafeMutableBufferPointer(start: pointer, count: 64)
for byte in buffer {
// ...
}
但这与内存的初始化方式无关。
在 Swift 3.1 中,UnsafeMutablePointer.initialize(from:) 已弃用。 Xcode 建议我改用 UnsafeMutableBufferPointer.initialize(from:)。我有一个看起来像这样的代码块:
let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
pointer.initialize(from: repeatElement(0, count: 64))
由于弃用,代码给了我一个编译时警告。所以我要把它改成:
let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
let buffer = UnsafeMutableBufferPointer(start: pointer, count: 64)
_ = buffer.initialize(from: repeatElement(0, count: 64))
这样做正确吗?我只是想确保我做的正确。
这是正确的,但是您可以使用
稍微简单地分配和初始化内存let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
pointer.initialize(to: 0, count: 64)
创建一个缓冲区指针视图仍然有用,因为
是一个集合,有一个count
属性 并且可以枚举:
let buffer = UnsafeMutableBufferPointer(start: pointer, count: 64)
for byte in buffer {
// ...
}
但这与内存的初始化方式无关。