title: Retrieve SecKey from Keychain
body: <p>I am trying to upgrade the code that I got from <a href="https://stackoverflow.com/a/37736209/5566326">this answer</a> for generating CSR, from Swift 2 to Swift 3. </p>
  
  <p>I have most of the code upgraded, but the following code in the Utility block of the original answer failed with the error:</p>
  
  <blockquote>
    <p>'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.</p>
  </blockquote>
  
  <p>The error occurs at the line:</p>
  
  <blockquote>
  <pre><code>let status: OSStatus = withUnsafeMutablePointer(to: &amp;dataTypeRef) { SecItemCopyMatching(query as NSDictionary, UnsafeMutablePointer($0)) }
  </code></pre>
  </blockquote>
  
  <pre><code>func loadKeySecKeyFromKeyChain(key: String) -&gt; SecKey{
      let query: Dictionary&lt;String, AnyObject&gt; = [
          String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
          String(kSecAttrKeySizeInBits): KEY_SIZE as AnyObject,
          String(kSecClass): kSecClassKey,
          String(kSecAttrApplicationTag): key as AnyObject,
          kSecReturnRef as String : kCFBooleanTrue ]
  
      var dataTypeRef: Unmanaged&lt;AnyObject&gt;? = nil
      var resultData: SecKey? = nil
  
      let status: OSStatus = withUnsafeMutablePointer(to: &amp;dataTypeRef) { SecItemCopyMatching(query as NSDictionary, UnsafeMutablePointer($0)) }
      NSLog("SecItemCopyMatching: " + status.description)
  
      if status == errSecSuccess {
          NSLog("private or public debug description is: " + dataTypeRef.debugDescription)
          resultData = (dataTypeRef!.takeRetainedValue() as! SecKey)
          NSLog("SecItemCopyMatching returns SecKey: " + resultData.debugDescription)
          return resultData!
      } else {
          return resultData!
      }
  }
  </code></pre>
  
  <p>I have been stuck on this for a whole day, is there any suggestions for how to resolve this error?</p>