我有以下代码写在
Swift 2.2中:
let keyData = NSMutableData(length: 64)! SecRandomCopyBytes(kSecRandomDefault,64,UnsafeMutablePointer<UInt8>(keyData.mutableBytes))
XCode 8强调第二行并声称
Cannot invoke initializer for type ‘UnsafeMutablePointer<_>’ with an
argument list of type ‘(UnsafeMutableRawPointer)’
虽然我感谢XCode告诉我,我不太明白如何更改UnsafeMutableRawPointer是可以接受的.
有谁知道如何将此代码转换为Swift 3?
解决方法
我建议您在Swift 3中使用Data而不是NSData.
var keyData = Data(count: 64) let result = keyData.withUnsafeMutableBytes {mutableBytes in SecRandomCopyBytes(kSecRandomDefault,keyData.count,mutableBytes) }
withUnsafeMutableBytes(_ :)被声明为通用方法,因此,在这种简单的情况下,可以使用它而不指定元素类型.