首页 > swift 中 Set 怎么通过index拿到 value?

swift 中 Set 怎么通过index拿到 value?

swift 中 Set 怎么通过index拿到 value? 我怎么就没找到这个方法? https://developer.apple.com/library/mac/documentation/Swift/Conceptual... 文档怎么也没找到?


应该是没有这种方法,但为啥还还提供startIndex endIndex 这俩方法 歧义


extension Set {
    subscript (index:Int) -> Element {
        return self[advance(self.startIndex, index)]
    }
}

给Set 扩展一个 Int 型的下标, 然后就可以通过 普通 Int 的 Index 来获取 value 了


首先你没有搞清楚一点,Set本身是无序的,所以你无法通过index拿到值,如果非要通过index拿的话,你可以使用sorted来进行排序(函数会根据一定规则进行排序的)然后用下标的方式来获取指定的value,但是这样做个人认为没什么意义

【热门文章】
【热门文章】