将多个对象的属性合并为一个

时间:2017-03-19 09:20:13

标签: swift

假设我有这门课程:

class Person {
    var firstName: String!
    var lastName: String!
    var phoneNumber: String?
    var email: String?
    var address: String?
}

现在我实例化了这个:

let person1 = Person()
person1.firstName = "Nicolas"
person1.lastName = "Cage"
person1.phoneNumber = "999-999-999"
person1.address = "No 123, NYC"

let person2 = Person()
person2.firstName = "Nicolas"
person2.lastName = "Cage"
person2.phoneNumber = "123-1234-1234"
person2.address = "No 123, NYC"
person2.email = "nicolas@nicolas.com"

let person3 = Person()
person3.firstName = "John"
person3.lastName = "Travolta"

我有这个阵列:

let actors: [Person] = [person1, person2, person3]

如何在没有冲突的情况下组合/合并重复人员,并且还有冲突属性列表?

合并后Nicoals Cage的预期属性值:

Nicolas
Cage
phoneNumber -> Conflict
No 123, NYC
nicolas@nicolas.com

1 个答案:

答案 0 :(得分:0)

private func merge(actors:[Person]) -> [Person] {

    var uniqueActors = [Person]()
    var duplicatedActors = [Person]()

    //split actors into arrays
    for actor in actors {
        if uniqueActors.contains(where: { $0.firstName == actor.firstName &&
                                            $0.lastName == actor.lastName}) {
            duplicatedActors.append(actor)
        } else {
            uniqueActors.append(actor)
        }
    }

    //merge duplicated into unique
    for duplicatedActor in duplicatedActors {
        let uniqueActor = uniqueActors.filter({ $0.firstName == duplicatedActor.firstName &&
                                                $0.lastName == duplicatedActor.lastName }).first
        if let uniqueActor = uniqueActor {
            uniqueActor.phoneNumber = self.mergeProperty(valueOne: uniqueActor.phoneNumber, valueTwo: duplicatedActor.phoneNumber)
            uniqueActor.address = self.mergeProperty(valueOne: uniqueActor.address, valueTwo: duplicatedActor.address)
            uniqueActor.email = self.mergeProperty(valueOne: uniqueActor.email, valueTwo: duplicatedActor.email)
        }
    }

    return uniqueActors

}

//merge properties of 2 objects
private func mergeProperty(valueOne: String?, valueTwo: String?) -> String? {
    var isFirstPropertyExist = false
    var isSecondPropertyExist = false

    if let valueOne = valueOne, valueOne != "" {
        isFirstPropertyExist = true
    }

    if let valueTwo = valueTwo, valueTwo != "" {
        isSecondPropertyExist = true
    }

    var mergedValue : String?

    switch (isFirstPropertyExist, isSecondPropertyExist) {
    case (true, true):
        if valueOne != valueTwo {
            mergedValue = "Conflict"
            //here you can print or throw an Error
        } else {
            mergedValue = valueOne
            //random (equal values)
        }

    case (true, false):
        mergedValue = valueOne
    case (false, true):
        mergedValue = valueTwo
    case (false, false):
        mergedValue = ""
        //or you can return nil
    }

    return mergedValue
}

我认为它可以帮到你。 您可以将func mergeProperty传递给Person类的扩展,将func merge传递给Array类的扩展,其中object是Person