withUnsafeMutableBytes deprecated in Swift 5?

Hi,

 

My code contains the following function:

 

  func md5(string: String) -> String {
  let messageData = string.data(using:.utf8)!
  var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH))
            
  _ = digestData.withUnsafeMutableBytes {digestBytes in
       messageData.withUnsafeBytes {messageBytes in
       CC_MD5(messageBytes, CC_LONG(messageData.count), digestBytes)
       }
  }
            
       let md5Hex = digestData.map { String(format: "%02hhx", $0) }.joined()
       return md5Hex
  }

 

This worked fine and compiled with no errors or warnings in prior versions of Swift. After upgrading to Swift 5, I get a deprecation warning on both “withUnsafeMutableBytes” and “withUnsafeBytes”.

 

I’ve stared at the warning, read the documentation, and even tried re-keying the code allowing Xcode to fill in what it thinks is the correct syntax, but I cannot resolve the problem. I do not understand what the complaint is, nor what I specifically need to type here to avoid the warnings.

 

Can someone please explain this, or at least, tell me what I need to write to get it correct?

 

Thanks.

Powered by WPeMatico

About

You may also like...

Comments are closed.