In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated

Jake

In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated. Xcode suggests I use UnsafeMutableBufferPointer.initialize(from:) instead. I have a code block that looks like this:

let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
pointer.initialize(from: repeatElement(0, count: 64))

The code gives me a compile time warning because of the deprecation. So I'm going to change that to:

let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
let buffer = UnsafeMutableBufferPointer(start: pointer, count: 64)
_ = buffer.initialize(from: repeatElement(0, count: 64))

Is this the right way to do this? I just wanted to make sure that I'm doing it correctly.

Martin R

It is correct, but you can allocate and initialize memory slightly simpler with

let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64)
pointer.initialize(to: 0, count: 64)

Creating a buffer pointer view can still be useful because that is a collection, has a count property and can be enumerated:

let buffer = UnsafeMutableBufferPointer(start: pointer, count: 64)

for byte in buffer {
    // ...
}

but that is independent of how the memory is initialized.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

UnsafeMutablePointer<CFTypeRef> in Swift 3

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

Error in UnsafeMutablePointer in swift3

How to use UnsafeMutablePointer in Swift 3?

UnsafeMutablePointer<Bytef> swift3

'++' is deprecated: it will be removed in Swift 3

++ is deprecated it will be removed in swift 3

CGRectDivide is deprecated in swift 3

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

Casting a variable of type 'UnsafeMutableRawPointer' to UnsafeMutablePointer<> in Swift 3

How to use UnsafeMutablePointer<T?> in swift 3

How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

How to do pointer arithmetic on UnsafeMutablePointer and get String in Swift 3

How to convert String to UnsafeMutablePointer<UInt8> ? Swift 3

'var' parameters are deprecated and will be removed in Swift 3

Swift 2 - UnsafeMutablePointer<Void> to object

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

Cast a Swift struct to UnsafeMutablePointer<Void>

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?

How to use UnsafeMutablePointer<OpaquePointer> in Swift?

How to represent Objc-C NSUInteger[] = {0,1,2} in Swift as UnsafeMutablePointer<UInt>?

How to convert a type of "any" in kind "UnsafeMutablePointer<Int16>" in a swift 3

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

'init(start:end:)' is deprecated: it will be removed in Swift 3. Use the '..<' operator

Fix warning "C-style for Statement is deprecated" in Swift 3

Swift convert Data to UnsafeMutablePointer<Int8>

Cast a swift dictionary ( [String:AnyObject] ) to a UnsafeMutablePointer

How to create UnsafeMutablePointer<CGPoint> Object in swift