美文网首页
swift 数组去重

swift 数组去重

作者: 一如初见丿 | 来源:发表于2017-02-24 17:25 被阅读280次

    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]

    相关文章

      网友评论

          本文标题:swift 数组去重

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