如何将值从一个数组添加到另一个数组?

时间:2017-06-26 21:11:52

标签: arrays swift

我有两个数组,其中一个必须完成另一个:

struct Score {
    let name: String
    let isComplete: Bool
    let finalScore: Int
}

var scores = [
    Score(name: "Steven", isComplete: false, finalScore: 12),
    Score(name: "Helen", isComplete: false, finalScore: 12),
    Score(name: "Adalbert", isComplete: false, finalScore: 12),
    Score(name: "Christian", isComplete: false, finalScore: 12),
    Score(name: "Susan", isComplete: false, finalScore: 3),
    Score(name: "Julia", isComplete: false, finalScore: 13),
    Score(name: "Robert", isComplete: false, finalScore: 13),
    Score(name: "Mercedes", isComplete: false, finalScore: 2),
    Score(name: "Gwendolin", isComplete: false, finalScore: 2),
]



struct Complete {
    let name: String
    let isComplete: Bool
}

var complete = [
    Complete(name: "Steven", isComplete: true),
    Complete(name: "Helen", isComplete: true),
    Complete(name: "Mercedes", isComplete: false),
    Complete(name: "Gwendolin", isComplete: true),
    // Name doesn´t exist in `score` and shouldn´t show up in the result:
    Complete(name: "nonexistingName", isComplete: true)
]

我怎么能用结果如下所示的方式用第二个数组完成第一个数组:

let result = [
    Score(name: "Steven", isComplete: true, finalScore: 12),
    Score(name: "Helen", isComplete: true, finalScore: 12),
    Score(name: "Adalbert", isComplete: false, finalScore: 12),
    Score(name: "Christian", isComplete: false, finalScore: 12),
    Score(name: "Susan", isComplete: false, finalScore: 3),
    Score(name: "Julia", isComplete: false, finalScore: 13),
    Score(name: "Robert", isComplete: false, finalScore: 13),
    Score(name: "Mercedes", isComplete: false, finalScore: 2),
    Score(name: "Gwendolin", isComplete: true, finalScore: 2),
]

通常我会发布我的尝试草稿,但由于我对swift很新,我不得不承认我不知道。谢谢你的任何建议。

2 个答案:

答案 0 :(得分:3)

最终你应该重新思考你的模特,可能还有某种类型的玩家"模型,所以你没有这种断开连接。

FWIW这里用字典做更安全/更简单的方法:

struct Score {
    let name: String
    var isComplete: Bool
    let finalScore: Int
}

struct Complete {
    let name: String
    let isComplete: Bool
}

var scoresDictionary: [String : Score] = [:]

// this populates scoresDictionary with your array, keyed with the names
[   Score(name: "Steven", isComplete: false, finalScore: 12),
    Score(name: "Helen", isComplete: false, finalScore: 12),
    Score(name: "Adalbert", isComplete: false, finalScore: 12),
    Score(name: "Christian", isComplete: false, finalScore: 12),
    Score(name: "Susan", isComplete: false, finalScore: 3),
    Score(name: "Julia", isComplete: false, finalScore: 13),
    Score(name: "Robert", isComplete: false, finalScore: 13),
    Score(name: "Mercedes", isComplete: false, finalScore: 2),
    Score(name: "Gwendolin", isComplete: false, finalScore: 2),
    ].forEach { scoresDictionary[$0.name] = $0}

let complete = [
    Complete(name: "Steven", isComplete: true),
    Complete(name: "Helen", isComplete: true),
    Complete(name: "Mercedes", isComplete: false),
    Complete(name: "Gwendolin", isComplete: true),
    // Name doesn´t exist in `score` and shouldn´t show up in the result:
    Complete(name: "nonexistingName", isComplete: true)
]

//Updates only the found values in scoresDictionary
complete.forEach { scoresDictionary[$0.name]?.isComplete = $0.isComplete }

// The dictionary is updated, but in case you just want the Scores, this gives you an array
let result: [Score] = Array(scoresDictionary.values)

答案 1 :(得分:0)

基本上我们只是迭代scores,寻找匹配的Complete。如果我们找到一个,我们会返回Score,其中isComplete设置为truefalse。如果我们找不到,我们将原始Score保持不变。

var final: [Score] = scores.map { aScore in

    let comp = complete.first { aScore.name == $0.name }
    if let comp = comp {
        return Score(name: aScore.name, isComplete: comp.isComplete || aScore.isComplete, finalScore: aScore.finalScore)
    }
    return aScore
}

final.forEach {
    print($0.name, $0.isComplete, $0.finalScore)
}

打印:

Steven true 12
Helen true 12
Adalbert false 12
Christian false 12
Susan false 3
Julia false 13
Robert false 13
Mercedes false 2
Gwendolin true 2