swift 数组去重 有标识的
//: Playground - noun: a place where people can play
import UIKit
class Book {
var title:String = ""
init(title:String, hasUpdate:Bool) {
self.title = title
self.hasUpdate = hasUpdate
}
}
var array:[Book] = [Book]()
array.append(Book(title:"Cocoa"))
array.append(Book(title:"Cocoa"))
array.append(Book(title:"Swft"))
array.append(Book(title:"Cocoa"))
array.append(Book(title:"Coco"))
var result:[Book] = []
for (idx, obj) in array.enumerated() {
var bool : Bool = false
for (index, object) in result.enumerated() {
if obj.title == object.title {
bool = true
}
}
if !bool {
result.append(obj)
}
}
for item in result {
print(item.title)
}
打印结果 : Cocoa Swft Coco
后面的覆盖前面的
let array = [1, 2, 1,12, 2, 12]
var dictInts = Dictionary()
for number in array {
dictInts[String(number)] = number
}
var result = [Int]()
for value in dictInts.values {
result.append(value)
}
print(result)
打印结果 :[2, 1, 12]
网友评论