title: Trying to use KeychainItemWrapper by Apple "translated" to Swift
body: <p>Sigh, I have been working on this the whole afternoon... here is my nightmare:</p>
  
  <p>I am trying to use the KeychainItemWrapper made by Apple. But I "translated" its Objective-C codes to Swift:</p>
  
  <pre><code>import Foundation
  import Security
  class MyKeychainItemWrapper: NSObject {
  var keychainItemData: NSMutableDictionary?
  var genericPasswordQuery: NSMutableDictionary = NSMutableDictionary()
  
  init(identifier: String, accessGroup: String?) {
  
      super.init()
  
      // Begin Keychain search setup. The genericPasswordQuery leverages the special user
      // defined attribute kSecAttrGeneric to distinguish itself between other generic Keychain
      // items which may be included by the same application.
      genericPasswordQuery.setObject(kSecClassGenericPassword, forKey: kSecClass)
      genericPasswordQuery.setObject(identifier, forKey: kSecAttrGeneric)
  
      // The keychain access group attribute determines if this item can be shared
      // amongst multiple apps whose code signing entitlements contain the same keychain access group.
      println(accessGroup)
      if (!(accessGroup == nil)) {
          genericPasswordQuery.setObject(accessGroup!, forKey: kSecAttrAccessGroup)
      }
  
      // Use the proper search constants, return only the attributes of the first match.
      genericPasswordQuery.setObject(kSecMatchLimitOne, forKey: kSecMatchLimit)
      genericPasswordQuery.setObject(kCFBooleanTrue, forKey: kSecReturnAttributes)
  
      var tempQuery: NSDictionary = NSDictionary(dictionary: genericPasswordQuery)
  
      var outDictionary: Unmanaged&lt;AnyObject&gt;? = nil
  
      var status: OSStatus = SecItemCopyMatching(tempQuery as CFDictionaryRef, &amp;outDictionary)
      println(status == noErr)
  
      if (status == noErr) {
          // Stick these default values into keychain item if nothing found.
          resetKeychainItem()
  
          // Add the generic attribute and the keychain access group.
          keychainItemData!.setObject(identifier, forKey: kSecAttrGeneric)
  
          if (!(accessGroup == nil)) {
              keychainItemData!.setObject(accessGroup!, forKey: kSecAttrAccessGroup)
          }
      } else {
          // load the saved data from Keychain.
          keychainItemData = secItemFormatToDictionary(outDictionary?.takeRetainedValue() as NSDictionary)
      }
  }
  </code></pre>
  
  <p>Then in my app's AppDelegate.swift, I am trying to use it by:</p>
  
  <pre><code>import UIKit
  
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
  
      var window: UIWindow?
      var passwordItem: MyKeychainItemWrapper = MyKeychainItemWrapper(identifier: "Password", accessGroup: nil)
  ...
  </code></pre>
  
  <p>So, the initializer is called, but but but somehow, I ALWAYS, ALWAYS get </p>
  
  <blockquote>
    <p>Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/nSKoT.png" alt="enter image description here"></p>
  
  <p>I have tried commenting out the problem lines and then I get this error at another if():
  <img src="https://i.stack.imgur.com/ju2CV.png" alt="enter image description here"></p>
  
  <p>I even tried:</p>
  
  <pre><code>var mmm: Bool = (accessGroup == nil)
  if (!mmm) {
    genericPasswordQuery.setObject(accessGroup!, forKey: kSecAttrAccessGroup)
  }
  </code></pre>
  
  <p>But same error at the same place, i.e. if(..)</p>
  
  <p>I am now so confused. Did I miss something here or?</p>
  
  <p>Environment: Xcode6-beta6, iOS 8 beta 5 on a non-jailbroken iPhone 5.</p>