Closure definition mismatch

Greetings!

 I try to port a framework written in Swift 5 to Swift 4.2. And encounter syntax error I can't understand.
Code: 
Code Block
 private static func ccAESKeyWrap(
        rawKey: Data,
        keyEncryptionKey: Data) -> (data: Data?, status: Int32) {
        let alg = CCWrappingAlgorithm(kCCWRAPAES)
        var wrappedKeyLength: size_t = CCSymmetricWrappedSize(alg, rawKey.count)
        var wrappedKey = Data(count: wrappedKeyLength)
        let status = wrappedKey.withUnsafeMutableBytes { (wrappedKeyBytes: UnsafeMutablePointer) in
            rawKey.withUnsafeBytes { (rawKeyBytes: UnsafePointer) in
                keyEncryptionKey.withUnsafeBytes { (keyEncryptionKeyBytes: UnsafePointer) -> Int32 in
                    guard
                        let wrappedKeyBytes = wrappedKeyBytes.bindMemory(to: UInt8.self).baseAddress,
                        let rawKeyBytes = rawKeyBytes.bindMemory(to: UInt8.self).baseAddress,
                        let keyEncryptionKeyBytes = keyEncryptionKeyBytes.bindMemory(to: UInt8.self).baseAddress
                    else {
                        return Int32(kCCMemoryFailure)
                    }
                    return CCSymmetricKeyWrap(
                        alg,
                        CCrfc3394_iv,
                        CCrfc3394_ivLen,
                        keyEncryptionKeyBytes,
                        keyEncryptionKey.count,
                        rawKeyBytes, rawKey.count,
                        wrappedKeyBytes, &wrappedKeyLength)
                }
            }
        }
        guard status == kCCSuccess else {
            return (nil, status)
        }
        wrappedKey.removeSubrange(wrappedKeyLength..<wrappedKey.count)
        return (wrappedKey, status)
    }


On string 
Code Block
keyEncryptionKey.withUnsafeBytes { (keyEncryptionKeyBytes: UnsafePointer) -> Int32 in

I got the error:
Cannot convert value of type '(UnsafePointer<>) -> Int32' to expected argument type '(UnsafePointer<>) -> '_

 Could anyone tell me what do I do wrong ?

Swift 5 to Swift 4.2.

I can't understand what you are trying. What version of Xcode are you using?
I tested in 12.4 (Swift 5.2), and got error in
            let status = wrappedKey.withUnsafeMutableBytes { (wrappedKeyBytes: UnsafeMutablePointer) in

Generic parameter 'Pointee' could not be inferred

Does this really compile with 5.2 for you ?

Note: code would be more readable with better formatting:

Code Block
private static func ccAESKeyWrap(rawKey: Data, keyEncryptionKey: Data) -> (data: Data?, status: Int32) {
let alg = CCWrappingAlgorithm(kCCWRAPAES)
var wrappedKeyLength: size_t = CCSymmetricWrappedSize(alg, rawKey.count)
var wrappedKey = Data(count: wrappedKeyLength)
let status = wrappedKey.withUnsafeMutableBytes { (wrappedKeyBytes: UnsafeMutablePointer) in
rawKey.withUnsafeBytes { (rawKeyBytes: UnsafePointer) in
keyEncryptionKey.withUnsafeBytes { (keyEncryptionKeyBytes: UnsafePointer) -> Int32 in
guard
let wrappedKeyBytes = wrappedKeyBytes.bindMemory(to: UInt8.self).baseAddress,
let rawKeyBytes = rawKeyBytes.bindMemory(to: UInt8.self).baseAddress,
let keyEncryptionKeyBytes = keyEncryptionKeyBytes.bindMemory(to: UInt8.self).baseAddress
else {
return Int32(kCCMemoryFailure)
}
return CCSymmetricKeyWrap(
alg,
CCrfc3394_iv,
CCrfc3394_ivLen,
keyEncryptionKeyBytes,
keyEncryptionKey.count,
rawKeyBytes, rawKey.count,
wrappedKeyBytes, &wrappedKeyLength)
}
}
}
guard status == kCCSuccess else {
return (nil, status)
}
wrappedKey.removeSubrange(wrappedKeyLength..<wrappedKey.count)
return (wrappedKey, status)
}


In Swift 5 (CryptoKit), there is an withUnsafeBytes, whose signature is:
func withUnsafeBytes<R>( body: (UnsafeRawBufferPointer) throws -> R) rethrows-> R

In Swift 4 and Swift 5 (Foundation), there is a func whose signature is:
func withUnsafeBytes<ResultType, ContentType>(
body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
But not the CryptoKit one apparently.

I can't understand what you are trying.

Compile framework originally written in Swift 5 with Swift 4.2.

What version of Xcode are you using?

Xcode Version 10.1 (10B61)

I tested in 12.4 (Swift 5.2), and got error in

The situation can be reproduced in Xcode 10.1 (Swift 4.2.1)

Also, I would like to attach the screenshot of the Xcode with error, but the forum does not have such functionality.

Xcode Version 10.1 (10B61)

Why can't you use Xcode 12 ? That would be the simplest and safest way.

I could not reproduce the exact error in Xcode 10.1.
Could you post the compete code for the class, with the import statements as well.

Why can't you use Xcode 12 ? That would be the simplest and safest way.

Because binaries compiled in swift 4.2 are not compatible with swift 5. I have a binary framework dependency compiled with swift 4.2.1.

Code in attach.


I have a binary framework dependency compiled with swift 4.2.1.

That means you cannot build a binary for App Store, are you OK with it?
Thanks.

But fileprivate extension ContentEncryptionAlgorithm does not correspond to any base class. Nor KeyManagementAlgorithm.

Did you try to get the frameworks compiled in Swift 5 (asking the developers) ?
Without it you may face serious problems in the future.

That means you cannot build a binary for App Store, are you OK with it?

I did not know that. Could you please provide an official document confirm that ?

See this:
https://developer.apple.com/app-store/submissions/ 
https://developer.apple.com/ios/submit/
says:

Starting April, 2020, all apps submitted to the App Store will need to be built with Xcode 11. Xcode 11 requires macOS Mojave 10.14.3 or later.

Starting April 2021, all iOS and iPadOS apps submitted to the App Store must be built with Xcode 12 and the iOS 14 SDK.



And more here:
https://stackoverflow.com/questions/41891165/minimum-xcode-version-to-upload-to-app-store

Thank you very much!

I did not know that. Could you please provide an official document confirm that ?

Submit your iOS and iPadOS apps to the App Store

Starting April 2021, all iOS and iPadOS apps submitted to the App Store must be built with Xcode 12 and the iOS 14 SDK.

Before April 2021, you can use Xcode 11+ and the iOS 13+ SDK, but not Xcode 10.1.
Thank you!
So, what's your choice? Continue developing with Xcode 10.1? Or migrate your framework to Xcode 12?

So, what's your choice? Continue developing with Xcode 10.1? Or migrate your framework to Xcode 12?

For now I have no reason to continue develop in deprecated version and I am going to rollback all of the changes and continue develop on Swift 5.
Closure definition mismatch
 
 
Q