How can I convert NSDictionary to NSData and vice versa?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting NSDictionary to NSData really means serializing a dictionary into bytes, and converting NSData back means deserializing those bytes into a dictionary again. The correct method depends on the format you want, usually JSON or property list.
The important rule is that not every dictionary can be serialized by every format. The dictionary contents must be compatible with the serializer you choose.
JSON Is the Most Common Choice
If the dictionary contains JSON-compatible values such as strings, numbers, arrays, dictionaries, booleans, and NSNull, JSONSerialization is usually the right tool.
Swift example:
And to convert the Data back:
That is the usual answer when you are preparing data for a network request or parsing JSON-like payloads.
Objective-C Version
If you are working in Objective-C with the exact NSDictionary and NSData types from the title, the same logic applies:
Deserialization:
This is the most direct Foundation-based approach.
When to Use Property Lists Instead
If the data is intended for Apple-only persistence or settings-like storage, property list serialization can be a better fit. Property lists support Foundation object graphs such as strings, numbers, dates, arrays, dictionaries, and data objects.
Swift example:
Use this when you want a property-list format rather than JSON, for example in Apple-specific storage or configuration use cases.
Bridging Between NSDictionary and Swift Types
In current Swift code, you often work with [String: Any] and Data rather than NSDictionary and NSData. The bridging is usually automatic:
And:
So even if the API title uses the Objective-C class names, the real serialization workflow in Swift is usually dictionary to Data and back.
Format Consistency Matters
If you encode as JSON, decode as JSON. If you encode as a property list, decode as a property list. The bytes do not carry enough context for Foundation to magically guess your intent.
This sounds obvious, but many bugs come from treating “bytes from a dictionary” as a generic concept when the actual format was never stated clearly.
Common Pitfalls
One common mistake is trying to serialize objects that are not valid JSON values, such as arbitrary custom classes. JSONSerialization only accepts JSON-compatible types.
Another mistake is assuming any NSData can be cast directly to NSDictionary. Raw bytes are not a dictionary until they are decoded using the same format that produced them.
It is also easy to ignore error handling. Serialization failures are common when the object graph contains unsupported values, so do and catch or NSError checks matter.
Finally, if you only need local secure persistence of custom objects, Codable or keyed archiving may be a better design than manually round-tripping through NSDictionary.
Summary
- Convert
NSDictionarytoNSDataby serializing it, usually withJSONSerializationorPropertyListSerialization. - Convert
NSDataback by deserializing with the matching format. - JSON is the usual choice for network-friendly dictionary data.
- Property lists are useful for Apple-specific storage and configuration formats.
- Make sure the dictionary contents are compatible with the serializer you choose.

