How can I check if a key exists in a dictionary? My dictionary is of type [Type:Type?]
.
I can\'t simply check dictionary[key] == nil
, as th
I believe the Dictionary type's indexForKey(key: Key)
is what you're looking for. It returns the index for a given key, but more importantly for your proposes, it returns nil if it can't find the specified key in the dictionary.
if dictionary.indexForKey("someKey") != nil {
// the key exists in the dictionary
}
Swift 3 syntax....
if dictionary.index(forKey: "someKey") == nil {
print("the key 'someKey' is NOT in the dictionary")
}