Appending NSDictionary to other NSDictionary
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You do not truly append one NSDictionary to another because NSDictionary is immutable. The usual solution is to create a mutable copy, add entries from the second dictionary, and optionally convert the result back to an immutable dictionary. The important part is deciding what should happen when both dictionaries contain the same key.
Merge with NSMutableDictionary
In Objective-C, the standard pattern is to start with one dictionary, create a mutable copy, and call addEntriesFromDictionary:.
This produces one combined dictionary containing keys from both inputs.
Understand Key Collision Behavior
If the same key exists in both dictionaries, the later dictionary wins. That means:
The merged result contains @"role": @"Manager". This behavior is often correct, but it should be intentional rather than accidental.
Return an Immutable Result When Appropriate
If the merged dictionary should not be modified later, convert it back to an immutable NSDictionary.
This is common when the merge is just a construction step and the final value should be treated as stable configuration or payload data.
Build a Convenience Helper
If dictionary merging happens often, wrap it in a helper so key overwrite behavior is consistent.
Using a helper prevents merge logic from being repeated with slightly different assumptions across the codebase.
Swift Equivalent
The same concept exists in Swift dictionaries, which are value types rather than Foundation reference types. Swift offers a built-in merging API.
The closure controls how key conflicts are resolved. In this example, the value from the second dictionary wins, which matches addEntriesFromDictionary: semantics.
Choose Merge Behavior Deliberately
Dictionary merging is easy mechanically, but domain meaning still matters. When duplicate keys appear, there are several possible rules:
- second dictionary overwrites the first
- first dictionary keeps priority
- conflicting keys trigger an error or assertion
The right rule depends on whether the dictionaries represent defaults, user overrides, request metadata, or something else. The API makes merging easy, but the business rule is yours to define.
Shallow Merge Versus Deep Merge
addEntriesFromDictionary: performs a shallow merge. If a key contains another dictionary as its value, the nested dictionary is replaced as one whole object. It is not merged recursively.
That matters for configuration trees and nested JSON-like structures. If you need recursive merging, you have to implement that behavior explicitly.
Common Pitfalls
- Trying to mutate an
NSDictionarydirectly instead of usingNSMutableDictionary. - Forgetting that duplicate keys are overwritten by the later dictionary.
- Returning a mutable dictionary when the caller expects an immutable result.
- Assuming nested dictionaries will be merged deeply when only a shallow merge is happening.
- Repeating merge logic inline instead of centralizing the key-collision rule.
Summary
- '
NSDictionaryis immutable, so merging starts with a mutable copy.' - '
addEntriesFromDictionary:is the standard Objective-C way to combine two dictionaries.' - If keys overlap, the values from the later dictionary overwrite earlier ones.
- Convert back to
NSDictionarywhen the final result should be immutable. - In Swift,
mergingprovides the same idea with explicit conflict handling.

