I need to check if an dict has a key or not. How?
Main Difference is:NSMutableDictionary is derived from NSDictionary, it has all the methods of NSDictionary. NSMutableDictionary is mutable( can be modified) but NSDictionary is immutable (can not be modified).
Sending a message to nil / NULL is always valid in Objective-C. So it depends if you want to check if a dictionary is nil , or if it doesn't have values (as a valid dictionary instance may be empty). In the first case, compare with nil . Otherwise, checks if count is 0, and ensure you're instance is still valid.
NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.
An NSDictionary will retain it's objects, and copy it's keys.
objectForKey
will return nil if a key doesn't exist.
if ([[dictionary allKeys] containsObject:key]) { // contains key }
or
if ([dictionary objectForKey:key]) { // contains object }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With