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

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

我第一次尝试在 swift 中使用 CommonCrypto(在 https://github.com/sergejp/CommonCrypto 的帮助下)。这是我的代码:

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

错误是:

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

<_> 是什么意思?我需要做什么?谢谢。

它抱怨的是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)
    }