Cannot Invoke Initializer for Type Unsafemutablepointer<Uint8>

Cannot invoke initializer for type UnsafeMutablePointerUInt8

You'd better use Data also for the result hash.

In Swift 3, withUnsafePointer(_:) and withUnsafeMutablePointer(:_) are generic types and Swift can infer the Pointee types correctly when used with "well-formed" closures, which means you have no need to convert Pointee types manually.

func withUnsafeBytes((UnsafePointer) -> ResultType)

func withUnsafeMutableBytes((UnsafeMutablePointer) -> ResultType)

func SHA256(data: String) -> Data {
var hash = Data(count: Int(CC_SHA256_DIGEST_LENGTH))

if let newData: Data = data.data(using: .utf8) {
_ = hash.withUnsafeMutableBytes {mutableBytes in
newData.withUnsafeBytes {bytes in
CC_SHA256(bytes, CC_LONG(newData.count), mutableBytes)
}
}
}

return hash
}

In Swift 3, the initializers of UnsafePointer and UnsafeMutablePointer, which was used to convert Pointee types till Swift 2, are removed. But in many cases, you can work without type conversions of pointers.

Cannot invoke initializer for type 'UnsafeMutablePointer'uint8' with an argument list of type '(UnsafeMuatableRawpointer?)'

Converting from void pointer (aka UnsafeMutableRawPointer) has changed in Swift 3. You have 2 options:

If you know your buffer length (safer):

let dataBuffer = src_buff?.bindMemory(to: UInt8.self, capacity: len)

If you don't know it:

let dataBuffer = src_buff?.assumingMemoryBound(to: UInt8.self)

Cannot invoke initializer for type 'UnsafeMutablePointer'

Check the type of info property from the latest reference:

Declaration

var info: UnsafeMutableRawPointer?

And the type of toOpaque() has become UnsafeMutableRawPointer.
(I couldn't have found an up-to-date Apple document, but you can check it easily in the Quick Help pane of Xcode.)

You have no need to convert:

    context.info = Unmanaged.passUnretained(self).toOpaque()

Cannot invoke initializer for type UnsafePointer_ with an argument list of type (UnsafeMutableRawPointer)

Please check the official reference when some sort of spec changes has affected with your code. In your case AudioBuffer.mData is of type UnsafeMutableRawPointer?, and you need to pass it to the first argument of OutputStream.write(_:maxLength:) of type UnsafePointer<UInt8>.

UnsafeMutableRawPointer

You can find this method which returns UnsafeMutablePointer<T>:

func assumingMemoryBound<T>(to: T.Type)

The concept of bound is sort of confusing, but seems you can use it for pointer type conversion:

outputStreme?.write(buffer.mData!.assumingMemoryBound(to: UInt8.self), maxLength: Int(buffer.mDataByteSize))

(Assuming forced-unwrapping ! is safe enough as suggested by your print(buffer.mData!).)

Memory bound-ness is not well defined for most APIs which return pointers, and has no effect as for now. There's another type conversion method func bindMemory<T>(to: T.Type, capacity: Int), and both work without problems (again, as for now).

UnsafePointerUInt8 initializer in Swift 3

  1. In Swift 3, you cannot init an UnsafePointer using an UnsafeRawPointer.

    You can use assumingMemoryBound(to:) to convert an UnsafeRawPointer into an UnsafePointer<T>. Like this:

    var ptr = data.bytes.assumingMemoryBound(to: UInt8.self)
  2. Use debugDescription or distance(to:) to compare two pointer.

    while(ptr.debugDescription < endPtr.debugDescription)

    or

    while(ptr.distance(to:endPtr) > 0)

Cannot invoke initializer for type... Only while compiling for tests

After undoing my changes I realized that I had created a test class named "SearchedLocation", just as my class. So that's why I was getting that error only when compiling to run the tests.

Stupid error :(

How to use UnsafeMutablePointer in Swift 3?

I recommend you to work with Data rather than NSData in Swift 3.

var keyData = Data(count: 64)
let result = keyData.withUnsafeMutableBytes {mutableBytes in
SecRandomCopyBytes(kSecRandomDefault, keyData.count, mutableBytes)
}

withUnsafeMutableBytes(_:) is declared as a generic method, so, in simple cases such as this, you can use it without specifying element type.



Related Topics



Leave a reply



Submit