swift Swift项目相关

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

2016-07-20  本文已影响85人  南国青天
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]
上一篇 下一篇

猜你喜欢

热点阅读