Int不能转换为DictionaryIndex <string,string =“”>

时间:2016-02-25 18:59:56

标签: ios swift

大家好,我遇到了大麻烦。这是我的代码:

let listOfQuestionsAndAnswers = ["Who’s Paul?": "An American", "Who’s Joao?": "A Bresilian", "Who’s Riccardo?": "An Italian"]

@IBAction func answerButtonTapped(sender: AnyObject){

    for (Question, rightAnswer) in listOfQuestionsAndAnswers {

        questionField.text = listOfQuestionsAndAnswers[currentQuestionIndex]
           if currentQuestionIndex <= listOfQuestionsAndAnswers.count
             {
              currentQuestionIndex = (++currentQuestionIndex) % listOfQuestionsAndAnswers.count
              answerBut.setTitle("ANSWER", forState: UIControlState.Normal)
             }
           else
           {
            (sender as UIButton).userInteractionEnabled = false
           }

我收到错误Int不能转换为DictionaryIndex,我不明白这意味着什么。我不应该通过索引访问我的字典。

3 个答案:

答案 0 :(得分:0)

很难说,你想做什么。这是一个例子,如何通过不同的方式访问字典(键,值对的无序集合)

let dict = ["a":"A","b":"B"]
for (k,v) in dict {
    print(k,v)
}
/*
b B
a A
*/

dict.forEach { (d) -> () in
    print(d.0,d.1)
}
/*
b B
a A
*/

dict.enumerate().forEach { (e) -> () in
    print(e.index,e.element,e.element.0,e.element.1)
}
/*
0 ("b", "B") b B
1 ("a", "A") a A
*/

dict.indices.forEach { (di) -> () in
    print(dict[di],dict[di].0,dict[di].1)
}
/*
("b", "B") b B
("a", "A") a A
*/

dict.keys.forEach { (k) -> () in
    print(k,dict[k])
}
/*
b Optional("B")
a Optional("A")
*/

答案 1 :(得分:0)

所以这里发生了一些事情,我不会按照你想要的方式。首先你要尝试迭代这个字典的主要问题就像它是一个列表,即let list = [apple, banana, orange]这个列表有一个index,你可以像你正在做的那样迭代。

for fruit in list {
    print(fruit)
 }

 This would print:
 apple
 banana
 orange

其中dictionarys更基于key:value

 struct food {
     catigory:String()
     type:String()
 }

我建议你制作一份字典列表,但是你的数据结构有点不同,比如

 let listOfQuestionAnswers = 
   [["question":"Who’s Paul?","answer":"An American"], 
   ["question":"Who’s Joao?","answer":"A Bresilian"],
   ["question":"Who’s Riccardo?","answer": "An Italian"]]

所以这让你有一个字典列表每个字典有两个键(问题和答案),现在你可以遍历所有这些键,你的问题和答案将配对在一起。

或者您可以使struct代表您的问题答案组合,然后列出这些结构。这样做很好,因为您可以使用dot语法来访问struct

中的项目
 struct dictionaryStruct {
    var question:String
    var answer:String
 }

var listOfQuestionAnswers = [dictionaryStruct]()

func makeList(quest:String,answer:String){
    let dict = dictionaryStruct.init(question: quest, answer: answer)
    listOfQuestionAnswers.append(dict)
}

makeList("Who’s Paul?", answer: "An American")
makeList("Who’s Joao?", answer: "A Bresilian")
makeList("Who’s Riccardo?", answer: "An Italian")

for entry in listOfQuestionAnswers {
    print("\(entry.question), \(entry.answer)")
}    


---------- Console Output:
      Who’s Paul?, An American
      Who’s Joao?, A Bresilian
      Who’s Riccardo?, An Italian

如果您还有其他问题,请告诉我们?

要解决您的得分逻辑,您有两个列表。用户选择答案并且您已经知道问题的索引,因此您只需要检查他们的答案是否与同一索引处的问题答案相同。所以看起来有点像这样

if answer == listOfAnswers[currentQuestionIndex]{
    score ++
}

答案 2 :(得分:0)

这里的其他答案都是正确的,我特别喜欢Daniel Leonard的回答,因为它提供了一个很好的方式来组织你的问题和答案。

首先,我想说listOfQuestionsAndAnswers不是一个列表 - 它实际上是一个词典。特别是,它是Dictionary<String, String>,即它的键必须是一个字符串,它的值必须是一个字符串。

但不要担心! Dictionary类型符合协议CollectionType,这意味着我们可以使用&#39;传统&#39;意味着索引它。 意味着我们可以使用Int访问它。但我们可以使用Dictionary.Index类型的索引访问它。

这是怎么做到的?

  1. 从字典中抓取索引。
  2. 通过使用索引获取值来迭代内容。
  3. 致电index.successor()
  4. 获取下一个索引
  5. 检查索引是否无效,方法是检查 是否等于结束索引。
  6. <强> 代码

    // Not a list of questions, it's a dictionary.
    let questionsAndAnswers = ["Who’s Paul?": "An American", 
                               "Who’s Joao?": "A Bresilian", 
                               "Who’s Riccardo?": "An Italian"]
    
    var index = questionsAndAnswers.startIndex
    while index != questionsAndAnswers.endIndex {
        let question = questionsAndAnswers[index].0
        let answer = questionsAndAnswers[index].1
        print("Question: \(question); Answer: \(answer)")
        index = index.successor()
    }
    

    你可以看到,当我们使用索引访问字典的内容时,我们检索一个元组。 .0是关键,.1是值,在这种情况下,分别对应于问题和答案。

    注意:字典中的索引不能保证订购 - 它们每次都会以不同的顺序出现!如果你想要一个有序的集合,那么你应该使用一个数组。

相关问题