0

I'm trying to loop over every item stored in the keychain. I'm using the following code:

let query = [
    kSecReturnAttributes as String : kCFBooleanTrue as Bool,
    kSecMatchLimit as String : kSecMatchLimitAll as String,
]


let secItemClasses = [
    kSecClassGenericPassword,
    kSecClassInternetPassword,
    kSecClassCertificate,
    kSecClassKey,
    kSecClassIdentity
]


for secItemClass in secItemClasses {
    query.setValue(secItemClass as String, forKey: kSecClass as String)
    var result: AnyObject?
    SecItemCopyMatching(query, &result)
    print(result)
}

However, I get the following error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<__NSDictionaryI 0x7fce2b682b30> setValue:forUndefinedKey:]: this class is not key
 value coding-compliant for the key class.'

How do I fix this?

Tometoyou
  • 7,792
  • 12
  • 62
  • 108
  • Which line is causing the issue? – Larme May 13 '16 at 15:01
  • @Larme The `query.setValue(secItemClass as String, forKey: kSecClass as String)` one – Tometoyou May 13 '16 at 15:03
  • I don't speak Swift, but your issue is that `query` is immutable (you can't change/add/remove its values. That's the reason of your error message: NSDictionaryImmutable (that's what it meant). Use `var query ` instead of `let query` according to this question: http://stackoverflow.com/questions/24096096/immutable-mutable-collections-in-swift – Larme May 13 '16 at 15:05
  • @Larme Correct! Although I actually had to just explicitly set it's type to `NSMutableDictionary`. – Tometoyou May 13 '16 at 15:07

0 Answers0