如何正确使用 map 和 zip 来比较我的结构数组中的属性?

How do I correctly use map & zip to compare properties in my array of Structs?

我正在使用以下行将测验中的正确答案与用户提交的答案进行比较。

它 returns 一个布尔数组,然后我会搜索并计算他们得到了多少真和多少假(真 = 正确,假 = 不正确)。

let totalResults = map(zip(correctAnswers, userAnswers)){[=10=].0 == [=10=].1}

我将所有测验信息存储在一个结构数组中,例如 结构问题信息 {

    var question : String!
    var answer : Bool!
    var userAnswer : Bool!
    var explanation : String!


    init(question: String, answer: Bool, explanation: String!) {
        self.question = question
        self.answer = answer
        self.explanation = explanation
    }
}


// These are the questions that will be shuffled before the quiz begins
var quizQuestion =

 [
    questionInfo(question: "Question 0", answer: true, explanation: ""),
    questionInfo(question: "Question 1", answer: true, explanation: ""),
    questionInfo(question: "Question 2", answer: true, explanation: ""),
    questionInfo(question: "Question 3", answer: true, explanation: ""),
    questionInfo(question: "Question 4", answer: true, explanation: "")
                                                                        ]

我如何将结构数组中的信息输入到我的地图和高空滑索中。我尝试使用 for 循环将所有答案附加到一个数组,但它看起来很乱。

为了澄清,我想做这样的事情:

let totalResults = map(zip(quizQuestion[x].answer, quizQuestion[x].userAnswer)){[=12=].0 == [=12=].1}

如果所有信息都在单个 quizQuestion 中,则不需要 zip() 数组,只有 map():

let totalResults = map(quizQuestion) { [=10=].answer == [=10=].userAnswer }   // [Bool]

然后你可以用reduce()计算正确答案的数量:

let numberOfCorrectResults = reduce(totalResults, 0) { [=11=] + Int() } // Int

这是 Swift 1.2 语法,对于来自 Xcode 7 beta 的 Swift 2,它将是

let totalResults = quizQuestion.map { [=12=].answer == [=12=].userAnswer }   // [Bool]
let numberOfCorrectResults = totalResults.reduce(0) { [=12=] + Int() } // Int