Swift 2.1:从结构数组中删除重复项?

时间:2016-01-24 20:03:32

标签: macos duplicates swift2

我有一个结构数组(结构中没有任何字符串,所以我想我可以使用一个字符串数组,但我想我可能想稍后添加一些非字符串元素。)

    struct ensRow {
        var adLastName: String = ""
        var adMaidenName: String = ""
        var adFirstName: String = ""
        var adAge: String = ""
        var adLocation: String = ""
        var adDeathDate: String = ""
        var adPubDate: String = ""
        var adPubDate2: String = ""
        var adPubDate3: String = ""
        var adObitDate: String = ""
        var adObitDate2: String = ""
        var adObitDate3: String = ""
        var adSource: String = ""
        var adSection: String = ""
        var adPage: String = ""
        var adPhotoAvailable: String = ""
        var adUDF1: String = ""
        var adUDF2: String = ""
        var adUDF3: String = ""
        var adNotes: String = ""
        var adFrom: String = ""
    }
    var ensArray: [ensRow] = []

我需要在填充数组或填充数据后有效地删除重复项。我想也许我可以将它设置为一个设置对象,如果我可以使它们可以使用(不确定如何)以及是否有条件方法将成员添加到集合中。但是,理想情况下,我希望最后一个属性(adFrom)是可变的,同时仍然在所有其他属性中删除带有重复项的结构。有关如何最好地做到这一点的任何建议?谢谢!

1 个答案:

答案 0 :(得分:0)

这似乎有用 - 有点蛮力但相当慢,但是......

struct ensRow:Equatable {
    var adLastName: String = ""
    var adMaidenName: String = ""
    var adFirstName: String = ""
    var adAge: String = ""
    var adLocation: String = ""
    var adDeathDate: String = ""
    var adPubDate: String = ""
    var adPubDate2: String = ""
    var adPubDate3: String = ""
    var adObitDate: String = ""
    var adObitDate2: String = ""
    var adObitDate3: String = ""
    var adSource: String = ""
    var adSection: String = ""
    var adPage: String = ""
    var adPhotoAvailable: String = ""
    var adUDF1: String = ""
    var adUDF2: String = ""
    var adUDF3: String = ""
    var adNotes: String = ""
    var adFrom: String = ""
}

func ==(lhs: ensRow, rhs: ensRow) -> Bool {
    if lhs.adLastName != rhs.adLastName {
        return false
    }
    return lhs.adLastName == rhs.adLastName
        && lhs.adMaidenName == rhs.adMaidenName
        && lhs.adFirstName == rhs.adFirstName
        && lhs.adAge == rhs.adAge
        && lhs.adLocation == rhs.adLocation
        && lhs.adDeathDate == rhs.adDeathDate
        && lhs.adPubDate == rhs.adPubDate
        && lhs.adPubDate2 == rhs.adPubDate2
        && lhs.adPubDate3 == rhs.adPubDate3
        && lhs.adObitDate == rhs.adObitDate
        && lhs.adObitDate2 == rhs.adObitDate2
        && lhs.adObitDate3 == rhs.adObitDate3
        && lhs.adSource == rhs.adSource
        && lhs.adSection == rhs.adSection
        && lhs.adPage == rhs.adPage
        && lhs.adPhotoAvailable == rhs.adPhotoAvailable
        && lhs.adUDF1 == rhs.adUDF1
        && lhs.adUDF2 == rhs.adUDF2
        && lhs.adUDF3 == rhs.adUDF3
        && lhs.adNotes == rhs.adNotes
}