Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func optionalKeyValueJSON() {
- let jsonStr1 = #"[{"key1":"value1", "key2": "value2"}]"#
- let jsonStr2 = #"[{"key2": "value2"}]"#
- let jsonStr3 = #"[{"key1": null, "key2": "value2"}]"#
- struct SCodable: Codable {
- let key1: String?
- let key2: String
- }
- func decode(jsonStr: String) {
- do {
- let decoded = try JSONDecoder().decode([SCodable].self, from: Data(jsonStr.utf8))
- print("Success: \(decoded)")
- } catch {
- print("Error: \(error)")
- }
- }
- decode(jsonStr: jsonStr1) //Works with key1 optional
- decode(jsonStr: jsonStr2) //Works with key1 optional while key1 is not present in the JSON
- decode(jsonStr: jsonStr3) //Works with key1 optional while key1 is null, ie ~ nil
- }
- optionalKeyValueJSON()
- Outputs:
- $>Success: [SCodable(key1: Optional("value1"), key2: "value2")]
- $>Success: [SCodable(key1: nil, key2: "value2")]
- $>Success: [SCodable(key1: nil, key2: "value2")]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement