title: Keychain persistent data iOS
body: <p>I'm using keychain to save a persistent data.
  2 of the functions are</p>
  
  <pre><code>func savePassword(password: String, inKeychainItem: NSData?) -&gt; NSData?
  func getPasswordWithPersistentReference(persistentReference: NSData) -&gt; String?
  </code></pre>
  
  <p>I just want to make sure, If I use the savePassword function and get a NSData, where should I keep this NSData to keep it persistent to the next time I'll run the app? Is the userDefault is a safe place to keep this data?</p>
  
  <p>Edit: Those are the functions I'm using</p>
  
  <pre><code>func savePassword(password: String, inKeychainItem: NSData?) -&gt; NSData? {
          guard let passwordData = password.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return nil }
          var status = errSecSuccess
  
          if let persistentReference = inKeychainItem {
              // A persistent reference was given, update the corresponding keychain item.
              let query: [NSObject: AnyObject] = [
                  kSecValuePersistentRef : persistentReference,
                  kSecReturnAttributes : kCFBooleanTrue
              ]
              var result: AnyObject?
  
              // Get the current attributes for the item.
              status = SecItemCopyMatching(query, &amp;result)
  
              if let attributes = result as? [NSObject: AnyObject] where status == errSecSuccess {
                  // Update the attributes with the new data.
                  var updateQuery = [NSObject: AnyObject]()
                  updateQuery[kSecClass] = kSecClassGenericPassword
                  updateQuery[kSecAttrService] = attributes[kSecAttrService]
  
                  var newAttributes = attributes
                  newAttributes[kSecValueData] = passwordData
  
                  status = SecItemUpdate(updateQuery, newAttributes)
                  if status == errSecSuccess {
                      return persistentReference
                  }
              }
          }
  
  func getPasswordWithPersistentReference(persistentReference: NSData) -&gt; String? {
          var result: String?
          let query: [NSObject: AnyObject] = [
              kSecClass : kSecClassGenericPassword,
              kSecReturnData : kCFBooleanTrue,
              kSecValuePersistentRef : persistentReference
          ]
  
          var returnValue: AnyObject?
          let status = SecItemCopyMatching(query, &amp;returnValue)
  
          if let passwordData = returnValue as? NSData where status == errSecSuccess {
              result = NSString(data: passwordData, encoding: NSUTF8StringEncoding) as? String
          }
          return result
      }
  </code></pre>
  
  <p>Edit2: basically, I just want to check if I saved a password and if I did then I want to get it. For this I need a persistentReference. I'm getting one from the save password function. but where should I keep it? </p>