Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning ⚠️ in Xcode 10.2.1 Swift 5.0.1 #54

Open
PH9 opened this issue Jun 28, 2019 · 3 comments
Open

Warning ⚠️ in Xcode 10.2.1 Swift 5.0.1 #54

PH9 opened this issue Jun 28, 2019 · 3 comments

Comments

@PH9
Copy link

PH9 commented Jun 28, 2019

There are about 38 warning in SwCrypt.swift file.

'withUnsafeMutableBytes' is deprecated: use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead
@PH9
Copy link
Author

PH9 commented Jun 28, 2019

This may fix with #51

@skito
Copy link

skito commented Aug 12, 2019

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

@everwanna
Copy link

Thank you @skito

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants