NSDictionary - Need to check whether dictionary contains key-value pair or not
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NSDictionary does not have a single built-in method that asks "does this exact key-value pair exist?" Instead, the normal approach is to look up the value for the key and then compare it to the value you expect.
Check the key first, then compare the value
Given a dictionary like this:
You can test for a specific key-value pair like this:
This works because dictionary lookup by key is fast, and isEqual: performs an object equality check that is appropriate for Foundation objects.
Why key lookup is the right approach
A dictionary is optimized around keys. If you already know the key you care about, scanning the entire dictionary is unnecessary. The pair exists only if:
- the key is present, and
- the value stored for that key equals the expected value.
That logic can be wrapped in a helper:
Usage:
Comparing numbers and booleans
Objective-C collections store scalar values inside wrapper objects such as NSNumber. That means comparisons should still use isEqual: rather than pointer equality:
Using == would compare object references, not the numeric value inside the NSNumber.
When you do not know the key
If you only know the value and want to find whether any key maps to it, you can inspect all entries:
This is more general, but it is slower than direct key lookup and usually unnecessary when the key is already known.
Be careful with nil and NSNull
NSDictionary cannot store nil as a value. If your data source uses null-like placeholders, it may store [NSNull null] instead. That means a missing key and a stored null marker are not the same thing.
That distinction matters when you are validating API responses or JSON-derived dictionaries.
Common Pitfalls
The biggest mistake is using == for object values such as strings or numbers. That checks pointer identity, not content equality, so two equal values may still compare false.
Another problem is assuming objectForKey: returning nil always means the key exists with a null-like value. In Foundation collections, nil means there was no object returned. Stored null placeholders are represented separately with NSNull.
Be careful when values are collections themselves. If the expected value is an array or nested dictionary, isEqual: still works, but you need to compare against an object of the same structure.
Finally, remember that NSDictionary is immutable. If your logic depends on adding or replacing pairs, switch to NSMutableDictionary.
Summary
- To test a key-value pair, look up the key and compare the returned object with
isEqual:. - Use direct key lookup when you already know the key.
- Compare
NSNumber,NSString, and other objects by value, not with==. - Distinguish between a missing key and an
NSNullplaceholder. - Enumerate the dictionary only when you do not know the key ahead of time.

