Swift - Cannot convert value of type 'UnsafePointer<Any>' to expected argument type 'UnsafePointer<_>'

ewizard picture ewizard · Dec 4, 2017 · Viewed 11.8k times · Source

I am trying to use CommonCrypto (with the help of https://github.com/sergejp/CommonCrypto) for the first time with swift. Here is my code:

UnsafeRawPointer(ivData!.withUnsafeBytes
{(pointer) -> UnsafePointer<Any> in
    let ivBuffer = pointer
})

The error is:

Cannot convert value of type 'UnsafePointer' to expected argument type 'UnsafePointer<_>'

What does the <_> signify? What do I need to do? Thanks.

Answer

Don picture Don · Dec 4, 2017

It's pointer that it is complaining about. You need to cast it. Here's an example usage, part of creating an MD5 hash:

    var rawBytes = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
    let _ = data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in
        CC_MD5(bytes, CC_LONG(data.count), &rawBytes)
    }