Swift Gist Favorites of 2015

Taking a look back recently, I realized a lot of my Swift sharing has been in the form of gists and less in the form of blog. As thus, here is a breakdown of my top three Swift gists since 2.0 came out.

3 – Optional Array Accessor

This is an array accessor that returns nil if the index is out of bounds. I use the &[index] syntax to mimic the unsafe arithmetic operators indicating operations are over/underflow-able without an error in Swift. i.e &+, &*,…

2 – Nested Keys in Dictionaries

The task here is to use a dot separated string as an accessor to navigate a series of nested dictionaries. My idea here was to use reduce to traverse the nested dictionaries. This gist came about from a conversation with @matthewcheok, who was looking for the best way to achieve this kind of functionality for use in JSONCodable.

let key = “a.b.c.d”
let dict = [ “a”:[“b”:[“c”:[“d”:”Data”]]]]

1 – Reduce Without Initial Value

There are some cases where the requirement for reduce to have an initial value takes away from its usefulness. This is an implementation called list that doesn’t need one! Making it throwable was necessary for handling the edge case of calling list on an empty collection.