简体   繁体   English

withUnsafeMutableBytes' 已弃用 xcode 警告

[英]withUnsafeMutableBytes' is deprecated xcode warning

I have 3 places (in total 7 warnings) in the project where XCode shows the next warning:我在 XCode 显示下一个警告的项目中有 3 个地方(总共 7 个警告):

withUnsafeMutableBytes' is deprecated withUnsafeMutableBytes' 已弃用

I tried on my own to resolves these warnings, but with no luck.我尝试自己解决这些警告,但没有运气。 Can someone help me?有人能帮我吗?

1. let status = cryptData.withUnsafeMutableBytes {ivBytes in
        SecRandomCopyBytes(kSecRandomDefault, kCCBlockSizeAES128, ivBytes)
    }

2. let cryptStatus = cryptData.withUnsafeMutableBytes {cryptBytes in
        data.withUnsafeBytes {dataBytes in
            keyData.withUnsafeBytes {keyBytes in
                CCCrypt(CCOperation(kCCEncrypt),
                        CCAlgorithm(kCCAlgorithmAES),
                        options,
                        keyBytes,
                        keyLength,
                        cryptBytes,
                        dataBytes,
                        data.count,
                        cryptBytes + kCCBlockSizeAES128,
                        cryptLength,
                        &numBytesEncrypted)
            }
        }
    }

3. let cryptStatus = clearData.withUnsafeMutableBytes {cryptBytes in
        data.withUnsafeBytes {dataBytes in
            keyData.withUnsafeBytes {keyBytes in
                CCCrypt(CCOperation(kCCDecrypt),
                        CCAlgorithm(kCCAlgorithmAES128),
                        options,
                        keyBytes,
                        keyLength,
                        dataBytes,
                        dataBytes + kCCBlockSizeAES128,
                        clearLength,
                        cryptBytes,
                        clearLength,
                        &numBytesDecrypted)
            }
        }
    }

As @Enricoza points out, the now-deprecated parameter given to the withUnsafeMutableBytes closure is of type UnsafeMutablePointer , and the new one is UnsafeMutableRawBufferPointer .正如@Enricoza 指出的那样,现在已弃用的参数给 withUnsafeMutableBytes 闭包是UnsafeMutablePointer类型,而新的参数是UnsafeMutableRawBufferPointer In order to get the UnsafeMutablePointer or UnsafeMutableRawPointer that CommonCrypto can accept, then you need to get the .baseAddress optional property on the *Bytes objects you get from your closures.为了获得 CommonCrypto 可以接受的UnsafeMutablePointerUnsafeMutableRawPointer ,您需要在从闭包中获得的*Bytes对象上获取.baseAddress可选属性。

Here's a library that uses the new closure parameters: https://github.com/backslash-f/aescryptable/blob/master/Sources/AESCryptable/AESCryptable.swift这是一个使用新闭包参数的库: https : //github.com/backslash-f/aescryptable/blob/master/Sources/AESCryptable/AESCryptable.swift

You'll note that the main difference is that it unwraps the baseAddress es in a guard clause, throws an error if any fail, then uses those baseAddress es in place of the old parameters to CommonCrypto functions.您会注意到主要区别在于它在guard子句中baseAddress es,如果有任何失败则抛出错误,然后使用这些baseAddress es 代替 CommonCrypto 函数的旧参数。

If you command+click on your method's call you'll see that you are using a deprecated method that unfortunately has the same name of the new method, but has different param types:如果你命令+点击你的方法调用,你会看到你正在使用一个已弃用的方法,不幸的是它与新方法具有相同的名称,但具有不同的参数类型:

    /// Mutate the bytes in the data.
    ///
    /// This function assumes that you are mutating the contents.
    /// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure.
    @available(swift, deprecated: 5, message: "use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead")
    public mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType

    @inlinable public mutating func withUnsafeMutableBytes<ResultType>(_ body: (UnsafeMutableRawBufferPointer) throws -> ResultType) rethrows -> ResultType

To avoid the warning you should use the second method and (of course) change the implementation of the closure (since it now has another type of parameter).为了避免警告,你应该使用第二种方法,并且(当然)改变闭包的实现(因为它现在有另一种类型的参数)。

To enforce the calling of the non-deprecated method you could simply force the type of the parameter in the closure like this:要强制调用未弃用的方法,您可以简单地强制闭包中的参数类型,如下所示:

let status = cryptData.withUnsafeMutableBytes { (ivBufferPointer: UnsafeMutableRawBufferPointer) in
        // Do your logic here with the UnsafeMutableRawBufferPointer
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM