title: iOS Swift 5 Keychain Status -50 with error SecItemAdd
body: <p>I am trying to write a wrapper with following a tutorial to getting into <code>Security</code> module and <code>Keychain</code> api. The wrapper seems ok but when I trying to set a password into keychain I got an error with status number -50.</p>
  <pre><code>import Security 
  
  class SecureStore {
      private func setupQueryDictionary(forKey key: String) throws -&gt; [CFString: Any] {
          guard let keyData = key.data(using: .utf8) else {
              print(&quot;Error! Cannot convert the key to the expected format&quot;)
              throw SecureStoreError.invalidContent
          }
          
          let query: [CFString: Any] = [kSecClass: kSecClassGenericPassword,
                                        kSecAttrAccount: keyData]
          
          
          return query
      }
      
      func set(entry: String, forKey key: String) throws {
          guard !entry.isEmpty &amp;&amp; !key.isEmpty else {
              print(&quot;Cant add an empty string to the keychain&quot;)
              throw SecureStoreError.queryEmptyWhenSet
          }
          
          try removeEntry(forKey: key)
          
          var queryDictionary = try setupQueryDictionary(forKey: key)
          queryDictionary[kSecClass] = entry.data(using: .utf8)
          
          let status = SecItemAdd(queryDictionary as CFDictionary, nil)
          
          guard status == errSecSuccess else {
              print(&quot;status ==&gt; &quot;, status.description)
              
              throw SecureStoreError.failureOnWrite(status)
          }
      }
      
      func removeEntry(forKey key: String) throws {
          guard !key.isEmpty else {
              print(&quot;key must be valid&quot;)
              throw SecureStoreError.queryEmptyWhenDelete
          }
          
          let queryDictionary = try setupQueryDictionary(forKey: key)
          SecItemDelete(queryDictionary as CFDictionary)
          
      }
      
      func entry(forKey key: String) throws -&gt; String? {
          guard !key.isEmpty else {
              print(&quot;key must be valid&quot;)
              throw SecureStoreError.invalidContent
          }
          
          var queryDictionary = try setupQueryDictionary(forKey: key)
          queryDictionary[kSecReturnData] = kCFBooleanTrue
          queryDictionary[kSecMatchLimit] = kSecMatchLimitOne
          
          var data: AnyObject?
          
          let status = SecItemCopyMatching(queryDictionary as CFDictionary, &amp;data)
          
          guard status == errSecSuccess else {
              print(&quot;status ==&gt; &quot;, status)
              throw SecureStoreError.failureOnRead(status)
          }
          
          guard let itemData = data as? Data, let result = String(data: itemData, encoding: .utf8) else {
              return nil
          }
          
          return result
          
          
      }
  }
  
  
  enum SecureStoreError: Error {
      case invalidContent
      case queryEmptyWhenSet
      case queryEmptyWhenDelete
      case failureOnWrite(OSStatus)
      case failureOnRead(OSStatus)
  }
  
  extension SecureStoreError: LocalizedError {
      public var errorDescription: String? {
          switch self {
          case .invalidContent:
              return NSLocalizedString(&quot;Cannot convert to the .utf8 format&quot;, comment: &quot;&quot;)
          case .queryEmptyWhenSet:
              return NSLocalizedString(&quot;for set(:,:), parameters cannot be empty &quot;, comment: &quot;&quot;)
          case .queryEmptyWhenDelete:
              return NSLocalizedString(&quot;for removeEntry(:,:), parameters cannot be empty &quot;, comment: &quot;&quot;)
          case .failureOnWrite(let status):
              return NSLocalizedString(&quot;Error when write to keychain item \(status.description)&quot;, comment: &quot;&quot;)
          case .failureOnRead(let status):
              return NSLocalizedString(&quot;Error when read to keychain item \(status.description)&quot;, comment: &quot;&quot;)
          }
      }
  }
  </code></pre>
  <p>Wrapper seems OK to me but I cannot get the real problem behind the error.</p>
  <p>I've already done these:</p>
  <p>1- Add capabilities in Xcode target and set a group.</p>
  <p>2- Tried in the simulator, got the same error</p>
  <p>3- I've tried it with the main thread, no effect.</p>
  <p>This is my callers:</p>
  <pre><code>let security = SecureStore.init()
              
  do {
                  
     try security.set(entry: &quot;myPassword12345.12345&quot;, forKey: &quot;_passwordForFacebook&quot;)
                  
     let password = try security.entry(forKey: &quot;_passwordForFacebook&quot;)
                  
     print(&quot;password &gt; &quot;, password  )
                  
  } catch {
     print(&quot;error &gt; &quot;, error.localizedDescription)
  }
  </code></pre>
  <p>Thanks in advance.</p>