UnsafePointer no longer works in swift 3

After I convert from swift 2 to swift 3, an error appears for the below line

let value = UnsafePointer<UInt32>(array1).pointee

'init' is not available: use 'withMemoryRebound (to: capacity: _)' to temporarily view memory as another type of mock compatible.

in swift2 it looks like

let value = UnsafePointer<UInt32>(array1).memory

Can someone explain please? Sorry, I'm pretty new to swift3

After making changes to

let abc = UnsafePointer<UInt32>(array1).withMemoryRebound(to: <#T##T.Type#>, capacity: <#T##Int#>, <#T##body: (UnsafeMutablePointer<T>) throws -> Result##(UnsafeMutablePointer<T>) throws -> Result#>)

but still, what value should be included in the variable? Sorry, I have a search around, but too bad I can not find a solution

+4
source share
2 answers

You can try the following:

let rawPointer = UnsafeRawPointer(array1)
let pointer = rawPointer.assumingMemoryBound(to: UInt32.self)
let value = pointer.pointee

Raw pointer - a pointer to access untype data.

assumingMemoryBound(to:) UnsafeRawPointer UnsafePointer<T>.

: Swift 3.0

+6

array array, withUnsafeBufferPointer:

array.withUnsafeBufferPointer { buffer in
    // do something with 'buffer'
    // (if you need an UnsafePointer rather than an UnsafeBufferPointer,
    // you can access that via the buffer .baseAddress property)
}

, , .

0

Source: https://habr.com/ru/post/1656545/


All Articles