How to use UnsafeMutablePointer in Swift 3?

后端 未结 1 1762
花落未央
花落未央 2020-12-03 14:53

I have the following code written in Swift 2.2:

let keyData = NSMutableData(length: 64)!
SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer

        
相关标签:
1条回答
  • 2020-12-03 15:24

    I recommend you to work with Data rather than NSData in Swift 3.

    var keyData = Data(count: 64)
    let result = keyData.withUnsafeMutableBytes {mutableBytes in
        SecRandomCopyBytes(kSecRandomDefault, keyData.count, mutableBytes)
    }
    

    withUnsafeMutableBytes(_:) is declared as a generic method, so, in simple cases such as this, you can use it without specifying element type.

    0 讨论(0)
提交回复
热议问题