2.5 · JSON coder options, property lists, UserDefaults
You already know how Codable synthesises a decoder. This section is about everything around it: the strategies you configure, the errors you read, the older untyped APIs, and the two small stores every app uses.
Mental model. The coder’s defaults are designed for Swift-to-Swift round trips, not for real servers. Most bugs are a default that does not match the other side.
Configuring JSON coders
Key names, dates, output format, and reading the error when it fails.
- What does keyDecodingStrategy do, and when does convertFromSnakeCase not match?
By default a Swift property name must equal the JSON key. keyDecodingStrategy = .convertFromSnakeCase converts each incoming key (user_id) to lower camel case… - How are dates encoded and decoded in JSON, and why are they 31 years off?
JSON has no date type, so you choose a dateEncodingStrategy and dateDecodingStrategy. The default, .deferredToDate, writes a Double of seconds since the 2001… - Which JSONEncoder and JSONDecoder options matter beyond keys and dates?
On the encoder, outputFormatting controls the text. .sortedKeys makes the output deterministic. Without it dictionary keys come out in a changing order, which… - How do you read a DecodingError, and how do you decode tolerantly?
Decoding failures throw a DecodingError with four cases: keyNotFound, typeMismatch, valueNotFound and dataCorrupted. Each carries a Context whose codingPath…
JSON beyond Codable
The untyped route, and what it costs.
- When would you use JSONSerialization instead of Codable, and what does the untyped route cost?
JSONSerialization parses JSON into an untyped tree of Any: NSDictionary, NSArray, NSString, NSNumber and NSNull. It suits shapes you cannot model in a type (a…
Property lists
The format behind Info.plist and UserDefaults.
- What is a property list, and where do you meet one?
A property list is Apple’s simple serialisation format for small structured data: dictionaries and arrays of strings, numbers, booleans, dates and Data. You…
UserDefaults
Small settings, typed getters that lie, and what does not belong there.
- What is UserDefaults, what can it store, and why do its typed getters mislead?
UserDefaults is a small persistent key-value store, saved as a property list in the app’s Preferences. It stores only property-list types: String, Number… - What belongs in UserDefaults, and how do you design and test around it?
Keep small, non-sensitive preferences and flags in UserDefaults. Secrets and tokens go in the Keychain; big or queryable data goes in files, Core Data or…
More in Foundation & stdlib
- 2.1 · Collections, indexing, Sequence and lazy
- 2.2 · Date, Calendar, Locale, TimeZone, FormatStyle
- 2.3 · URL, URLComponents, Data, FileManager
- 2.4 · NotificationCenter, KVO and Operation
- 2.6 · Bundle, resources, localization tables
- 2.7 · NSCache and locks
- 2.8 · Attributed strings, scanners, number formatters
- 2.9 · Bridging to Objective-C Foundation types