Swift-无法将类型'UnsafePointer <Any>'的值转换为预期的参数类型'UnsafePointer <_>'

ewi*_*ard 1 encryption types pointers commoncrypto swift

我正在尝试快速使用CommonCrypto(在https://github.com/sergejp/CommonCrypto的帮助下)。这是我的代码:

UnsafeRawPointer(ivData!.withUnsafeBytes
{(pointer) -> UnsafePointer<Any> in
    let ivBuffer = pointer
})
Run Code Online (Sandbox Code Playgroud)

错误是:

无法将类型'UnsafePointer'的值转换为预期的参数类型'UnsafePointer <_>'

<_>意味着什么?我需要做什么?谢谢。

Don*_*Don 5

pointer是它在抱怨。您需要投射它。这是用法示例,它是创建MD5哈希的一部分:

    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)
    }
Run Code Online (Sandbox Code Playgroud)