ios - Comparing Unmanaged<AnyObject> to nil -


itemprop = "text">

The code has been copied from this blog post:

I had this code:

  // Keychain Discovery Item Search:! OSStatus = SecItemCopyMatching (keychainQuery, & amp; dataTypeRef) // following line EXEC_BAD_ACCESS if dataTypeRef zero bridesmaid opaque = dataTypeRef .toOpaque () with accidents  

When I was a new above keychain query account uses values ​​do, dataTypeRef will null that EXEC_BAD_ACCESS

I if dataTypeRef < / Code> is tried by trying to circumvent this zero this way:.

  var opaque = COpaquePointer.null () if (dataTypeRef! = Zero) {opaque = dataTypeRef! .toOpaque () // This line should not be executed. }  

While the debugger detects that dataTypeRef is zero it will still enter, the segment and crash.

Can anyone tell me what is happening here? I have experience in Objective-C but I can not understand what is happening here. As far as I know, toOpaque / fromOpaque

Dance is no longer necessary, and you can use alternate binding. It can be combined with optional chaining using NSData is:

  case: OSStatus = SecItemCopyMatching (keychainQuery, & amp; dataTypeRef ) Go to retrievedData = dataTypeRef? As the price () taken? NSData {contentsOfKeychain = NSString (data: retrievedData, encoding: NSUTF8StringEncoding)} Note  

Also takeRetainedValue () right choice here because you "own" SecItemCopyMatching (its name is "COPY")

But in reality you should check the back value first:

  to go:? OSStatus = SecItemCopyMatching (keychainQuery, & amp; dataTypeRef) If the status == errSecSuccess {if the retrievedData = dataTypeRef .takeRetainedValue (as)? Ansdita {content Ofkeican = Ansstiing (data to retrieve data encoding: Anstif 8 String encoding)}}  

Comments