大家好,我有大麻烦了。这是我的代码:
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/