简体   繁体   English

无法为类型UnsafeMutablePointer调用初始值设定项<UInt8>

[英]Cannot invoke initializer for type UnsafeMutablePointer<UInt8>

I'm trying to convert my string into SHA256 hash, but I get the next error: 我正在尝试将我的字符串转换为SHA256哈希,但我得到了下一个错误:

Cannot invoke initializer for type 'UnsafeMutablePointer<UInt8>' with an argument list of type '(UnsafeMutableRawPointer)'

That's my function: 那是我的功能:

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

    if let newData: Data = data.data(using: .utf8) {
        let bytes = newData.withUnsafeBytes {(bytes: UnsafePointer<CChar>) -> Void in
            CC_SHA256(bytes, CC_LONG(newData.length), UnsafeMutablePointer<UInt8>(hash.mutableBytes))
        }
    }

    return hash as Data
}

so, for this part 所以,对于这一部分

UnsafeMutablePointer<UInt8>(hash.mutableBytes)

I get this error: 我收到此错误:

Cannot invoke initializer for type 'UnsafeMutablePointer<UInt8>' with an argument list of type '(UnsafeMutableRawPointer)'

How can I fix that and what I do wrong? 我怎样才能解决这个问题以及我做错了什么?

You'd better use Data also for the result hash . 您最好也将Data用于结果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. 在Swift 3中, withUnsafePointer(_:)withUnsafeMutablePointer(:_)是泛型类型,当与“格式良好”的闭包一起使用时,Swift可以正确推断Pointee类型,这意味着您无需手动转换Pointee类型。

func withUnsafeBytes((UnsafePointer) -> ResultType) func withUnsafeBytes((UnsafePointer) - > ResultType)

func withUnsafeMutableBytes((UnsafeMutablePointer) -> 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. 在Swift 3中, UnsafePointerUnsafeMutablePointer的初始化UnsafeMutablePointer ,用于将Pointee类型转换为Swift 2。 But in many cases, you can work without type conversions of pointers. 但在许多情况下,您可以在没有指针类型转换的情况下工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二进制运算符+ =不能应用于类型&#39;UnsafeMutablePointer的操作数 <UInt8> &#39; 和&#39;Int&#39; - binary operator += cannot be applied to operands of type 'UnsafeMutablePointer<UInt8>?' and 'Int' 无法为类型&#39;UnsafeMutablePointer调用初始化程序 <Float> &#39;,其参数类型为&#39;(UnsafeMutableRawPointer!)&#39; - Cannot invoke initializer for type 'UnsafeMutablePointer<Float>' with an argument list of type '(UnsafeMutableRawPointer!)' 无法使用类型为&#39;(UInt32,UnsafePointer的参数列表&#39;调用&#39;CCHmac&#39; <Int8> ,UInt,[CChar],Int,UnsafeMutablePointer <CUnsignedChar> ) - Cannot invoke 'CCHmac' with an argument list of type '(UInt32, UnsafePointer<Int8>, UInt, [CChar], Int, UnsafeMutablePointer<CUnsignedChar>) 无法将“UInt”类型的值转换为预期的参数类型“UnsafeMutablePointer” <UInt> “ - Cannot convert value of type 'UInt' to expected argument type 'UnsafeMutablePointer<UInt>' 如何转换UnsafeMutablePointer <UInt8> 在Swift中使用UIImage - How to convert UnsafeMutablePointer<UInt8> to UIImage in Swift 不安全指针<UInt8> Swift 3 中的初始化程序 - UnsafePointer<UInt8> initializer in Swift 3 无法为类型为&#39;(UInt32)&#39;的参数列表调用类型&#39;CGBitmapInfo&#39;的初始化程序 - Cannot invoke initializer for type 'CGBitmapInfo' with an argument list of type '(UInt32)' 无法为UIPanGestureRecognizer类型调用初始化程序 - Cannot invoke initializer for type UIPanGestureRecognizer CoreGraphics-无法替换UnsafeMutablePointer <UInt32> 与UnsafeMutablePointer <UInt8> - CoreGraphics - Can't replace UnsafeMutablePointer<UInt32> with UnsafeMutablePointer<UInt8> 转换UnsafeMutablePointer时堆缓冲区溢出 <UInt8> 到字符串 - Heap Buffer Overflow when converting UnsafeMutablePointer<UInt8> to String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM