SwCrypt icon indicating copy to clipboard operation
SwCrypt copied to clipboard

Warning ⚠️ in Xcode 10.2.1 Swift 5.0.1

Open PH9 opened this issue 6 years ago • 3 comments

There are about 38 warning in SwCrypt.swift file.

'withUnsafeMutableBytes' is deprecated: use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead

PH9 avatar Jun 28 '19 08:06 PH9

This may fix with #51

PH9 avatar Jun 28 '19 08:06 PH9

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

skito avatar Aug 12 '19 19:08 skito

Thank you @skito

everwanna avatar Dec 07 '19 13:12 everwanna