ios - Int 不可转换为 DictionaryIndex<String, String>

标签 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 is not convertible to DictionaryIndex 并且我不明白这是什么意思。我不应该能够通过索引访问我的词典吗?

最佳答案

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

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")
*/

关于ios - Int 不可转换为 DictionaryIndex<String, String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635998/

相关文章:

iphone - 核心数据不获取记录

ios - 提交给 Apple 的应用程序在本地运行与在 Apple 使用 Parse.com 后端运行不同

ios - Objective-C 将导航栏添加到 rootview

ios - ARC 和非 ARC 框架之间的内存管理

javascript - 首次登录时的 React-Native 欢迎屏幕

swift - 使用 Swift 将最小/最大图像添加到 NSSlider

ios - 如何为 Collection View 创建多个数据源?

ios - 如何安装react-native-track-player

swift - 在 Swift 的单行上测试 nil

ios - 是否有开源的 Flurry 替代品?