Using SecRandomCopyBytes in Swift

You were close, but return inside the closure returns from the closure, not from the outer function. Therefore only SecRandomCopyBytes() should be called in the closure, and the result passed back. func generateRandomBytes() -> String? { var keyData = Data(count: 32) let result = keyData.withUnsafeMutableBytes { (mutableBytes: UnsafeMutablePointer<UInt8>) -> Int32 in SecRandomCopyBytes(kSecRandomDefault, 32, mutableBytes) } … Read more