Swift优雅的处理数组越界问题

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

//let label = labels[safe:i]

你可能感兴趣的:(Swift优雅的处理数组越界问题)