Briahas
5/14/2018 - 12:33 PM

safeIndex

extension Collection {
    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

example: array[safe: index]