Swift 数组去重(有序去重)
2018-07-17 本文已影响19人
子非鱼7868
今天的需求 需要对数组去重 写了下面的方法 留着备用
extension Array where Element:Hashable{
var unique : [Element] {
var keys:[Element:()] = [:]
return filter{keys.updateValue((), forKey:$0) == nil}
}
}
调用
let cityListArray = [1,2,2,3,4,5,4,3,1,6,7,8]
let cityArray = cityListArray.unique