从 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, length, data.mutableBytes)
但我遇到了这个编译器错误:
'Void' is not identical to 'UInt8'
由于类型不匹配,数据的mutableBytes参数被拒绝,但我无法强制转换该参数(并且我假设这样做是安全的)。
nil
代替kSecRandomDefault
。 - SulthanOverlapping accesses to 'data', but modification requires exclusive access; consider copying to a local variable
。解决方案是用你传递的<count>
替换data.count
。 - Andy Ibanez