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

13,778

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)
    }
Share:
13,778

Related videos on Youtube

ewizard
Author by

ewizard

Hold on, one sec, let me start my google machine...

Updated on June 04, 2022

Comments

  • ewizard
    ewizard almost 2 years

    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.

    • Nordeast
      Nordeast over 6 years
      Its saying that you cannot convert the type UnsafePointer to a UnsafePointer with a type. I think you need to remove <Any>. Above that it is hard to know with out more code as context.
    • ewizard
      ewizard over 6 years
      i get another error when I do this Reference to generic type 'UnsafePointer' requires arguments in <...> telling me to revert
  • ewizard
    ewizard over 6 years
    great thanks that did it....are the rawBytes always able to be cast as UInt8? I am doing AES encryption
  • Don
    Don over 6 years
    You can cast it to whatever you want. To be clear, the reason that you have to cast it is because you are returning it, and preBuffer will be whatever type you are casting it to. Take a look at the Swift 3 answer using Data at stackoverflow.com/questions/37680361/aes-encryption-in-swift‌​, it might provide more insight into how to use it.
  • Don
    Don over 6 years
    Side note: you should try to avoid using the force-unwrap operator ! where you can, by using if let or guard let constructs. Colloquially, ! is known as the "crash operator"
  • ewizard
    ewizard over 6 years
    yah im just figuring the optionals out...i have guard where i need it in most places except below i plan on putting that in
  • ewizard
    ewizard over 6 years
    great link...i was looking for something like that for a while to base my encryption function on