Skip to content

Commit

Permalink
Note about nested keys and the =>? operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Jun 2, 2016
1 parent adecd49 commit fdcfbfc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ For convenience there is an operator, `=>?`, that only returns nil on missing ke
| `=>? -> T?`| nil | nil | throws | uncaught (throws) |
| `try? => -> T `| nil | nil | nil | caught (nil) |

#### Note about nested keys and the `=>?` operator
Currently, either all keys in an expression throw on a missing key or none of them do.
```swift
let a: Int = try json => "user" => "followers" // Will throw if either key is missing
let b: Int = try json =>? "user" => "followers" // Won't throw if either key is missing
let c: Int = try json => "user" =>? "followers" // Won't compile
```
This is controlled by the left most operator (where the actual decoding happens). Subsequent `=>` only append keys to an array, and do not affect anything else.

This might be dealt with by #77.

## Tips
- You can use `Decodable` with classes. Just make sure to either call a `required` initializer on self (e.g `self.init`) and return `Self`, or make your class `final`. ( [This](http://stackoverflow.com/questions/26495586/best-practice-to-implement-a-failable-initializer-in-swift) might be a problem though)
Expand Down

0 comments on commit fdcfbfc

Please sign in to comment.