代码之家  ›  专栏  ›  技术社区  ›  Wazza

不推荐使用的数据函数

  •  0
  • Wazza  · 技术社区  · 6 年前

    我刚开始一个项目,遇到了一个不推荐使用的函数。在谷歌搜索之后,我找到了几种解决方案,但只针对错误的第一部分。我的问题是,我不知道如何处理同样不推荐使用的嵌套零件。功能如下:

    private static func compute(data: Data, operation: Int) -> Data {
        let cryptLength = size_t(data.count + kCCBlockSizeAES128)
        var cryptData = Data(count:cryptLength)
    
        let keyLength = size_t(kCCKeySizeAES128)
        let options = CCOptions(kCCOptionPKCS7Padding)
    
        var numBytesEncrypted: size_t = 0
    
        let cryptStatus = cryptData.withUnsafeMutableBytes { cryptBytes in  <--- Warning 1
            data.withUnsafeBytes { dataBytes in  <--- Warning 2
                Data(Encoded.iv).withUnsafeBytes { ivBytes in  <--- Warning 3
                    Data(Encoded.key).withUnsafeBytes { keyBytes in  <--- Warning 4
                        CCCrypt(CCOperation(operation),
                                CCAlgorithm(kCCAlgorithmAES),
                                options,
                                keyBytes, keyLength,
                                ivBytes,
                                dataBytes, data.count,
                                cryptBytes, cryptLength,
                                &numBytesEncrypted)
                    }
                }
            }
        }
    
        guard case UInt32(cryptStatus) == UInt32(kCCSuccess) = true else {
            print("Something went wrong: \(cryptStatus)")
            return cryptData
        }
    
        cryptData.removeSubrange(numBytesEncrypted..<cryptData.count)
    
        return cryptData
    }
    

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

    警告2,3+4: 'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead

    我试过了 cryptData.withUnsafeMutableBytes { (cryptBytes: UnsafeMutablePointer) in 或添加 try 但要么出现新的错误,要么警告保持不变。

    有人能帮忙吗?

    0 回复  |  直到 6 年前