title: Using SecRandomCopyBytes in Swift
body: <p>I want to generate random bytes using <code>SecRandomCopyBytes</code> in Swift 3.0. Here is how I did it in Swift 2.2</p>
  
  <pre><code>private static func generateRandomBytes() -&gt; String? {
      let data = NSMutableData(length: Int(32))
  
      let result = SecRandomCopyBytes(kSecRandomDefault, 32, UnsafeMutablePointer&lt;UInt8&gt;(data!.mutableBytes))
      if result == errSecSuccess {
          return data!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
      } else {
          print("Problem generating random bytes")
          return nil
      }
  }
  </code></pre>
  
  <p>In Swift 3, I tried to do it like this, since I know the concept of unsafemutablebytes is different now, but it doesn't allow me to return. If I comment out the return part, it still says <code>Generic Parameter ResultType could not be inferred</code></p>
  
  <pre><code>fileprivate static func generateRandomBytes() -&gt; String? {
      var keyData = Data(count: 32)
      _ = keyData.withUnsafeMutableBytes {mutableBytes in
          let result = SecRandomCopyBytes(kSecRandomDefault, keyData.count, mutableBytes)
          if result == errSecSuccess {
              return keyData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
          } else {
              print("Problem generating random bytes")
              return nil
          }
      }
      return nil
  }
  </code></pre>
  
  <p>Does anyone know how to fix this?</p>
  
  <p>Thanks</p>