美文网首页swift Swift项目相关
Swift优雅的处理数组越界问题

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

作者: 南国青天 | 来源:发表于2016-07-20 16:28 被阅读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]
    

    相关文章

      网友评论

        本文标题:Swift优雅的处理数组越界问题

        本文链接:https://www.haomeiwen.com/subject/gfivjttx.html