从Objective-C,我可以这样做:
NSMutableData *data = [NSMutableData dataWithLength:length]; int result = SecRandomCopyBytes(kSecRandomDefault,length,data.mutableBytes);
在Swift中尝试这个时,我有以下内容:
let data = NSMutableData(length: Int(length)) let result = SecRandomCopyBytes(kSecRandomDefault,data.mutableBytes)
但我得到这个编译器错误:
'Void' is not identical to 'UInt8'
data.mutableBytes参数被拒绝,因为类型不匹配,但我无法弄清楚如何强制参数(我假设它在某种程度上是安全的).
解决方法
这似乎有效:
let data = NSMutableData(length: Int(length)) let result = SecRandomCopyBytes(kSecRandomDefault,UnsafeMutablePointer<UInt8>(data.mutableBytes))