SwCrypt
SwCrypt copied to clipboard
Warning ⚠️ in Xcode 10.2.1 Swift 5.0.1
There are about 38 warning in SwCrypt.swift file.
'withUnsafeMutableBytes' is deprecated: use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead
This may fix with #51
No, the warnings are still popping up. The code needs to be refactored for Swift 5. Here's a an example:
fileprivate func withUnsafePointers<A0, A1, Result>(
_ arg0: Data,
_ arg1: inout Data,
_ body: (
UnsafePointer<A0>,
UnsafeMutablePointer<A1>) throws -> Result
) rethrows -> Result {
return try arg0.withUnsafeBytes { p0 in
let b0: UnsafePointer<A0> = p0.baseAddress!.assumingMemoryBound(to: A0.self)
return try arg1.withUnsafeMutableBytes { p1 in
let b1: UnsafeMutablePointer<A1> = p1.baseAddress!.assumingMemoryBound(to: A1.self)
return try body(b0, b1)
}
}
}
Basically you need to add another variable inside the call to parse either like UnsafePointer or UnsafeMutablePointer depend on whether the call is to withUnsafeBytes or withUnsafeMutableBytes respectivley.
Another example:
public static func generateRandom(_ size: Int) -> Data {
var data = Data(count: size)
data.withUnsafeMutableBytes { dataBytes in
let b0: UnsafeMutablePointer<UInt8> = dataBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)
_ = CCRandomGenerateBytes!(b0, size)
}
return data
}
You can read more about here
Full refactored file for v5.1.3 attached below. SwCrypt v5.1.3(refactored).swift.zip
Cheers, Dimitar
Thank you @skito