[英]Int is not convertible to Dictionary<string, string>
There is an error at this line (questionField.text = listOfQuestionsAndAnswers[currentQuestionIndex]) (Int is not convertible to Dictionary). 这行有一个错误(questionField.text = listOfQuestionsAndAnswers [currentQuestionIndex])(Int不能转换为Dictionary)。 Moreover, I want all the questions to be displayed one-by-one, and after the last question, "Who's is Paul" should be displayed again... 此外,我希望所有问题都一一显示,在最后一个问题之后,应该再次显示“谁是保罗”。
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
}
}
}
I am getting the error Int is not convertible to DictionaryIndex and I don't understand what that means. 我收到错误Int无法转换为DictionaryIndex,我不明白这意味着什么。 Shouldn't I be able to access my dictionary by index. 我不应该能够通过索引访问我的词典。
You can't subscript a dictionary by Int
. 您不能通过Int
为字典添加下标。 Dictionaries contain keys and values and are subscripted by the key. 字典包含键和值,并由键下标。 In this case your listOfQuestionsAndAnswers
is a Dictionary where the keys and values are both Strings. 在这种情况下,您的listOfQuestionsAndAnswers
是一个词典,其中的键和值均为字符串。
If you wanted to subscript by Int, consider using an array of (String, String)
tuples. 如果要按Int下标,请考虑使用(String, String)
元组数组。
If you want to use a dictionary, you have to retrieve the value from the dictionary by its key: 如果要使用字典,则必须通过其键从字典中检索值:
listOfQuestionsAndAnswers["Who’s Paul?"] // "An American"
listOfQuestionsAndAnswers is not an array is a Dictionary and listOfQuestionsAndAnswers[someIntIndex] is not working because your keys are strings listOfQuestionsAndAnswers不是数组是字典,并且listOfQuestionsAndAnswers [someIntIndex]不起作用,因为您的键是字符串
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]
//changed to this
questionField.text = Quesition
if currentQuestionIndex <= listOfQuestionsAndAnswers.count
{
currentQuestionIndex = (++currentQuestionIndex) % listOfQuestionsAndAnswers.count
answerBut.setTitle("ANSWER", forState: UIControlState.Normal)
}
else
{
(sender as UIButton).userInteractionEnabled = false
}
} }
Your listOfQuestionsAndAnswers is a dictionary of type Dictionary<String:String>
. 您的listOfQuestionsAndAnswers是Dictionary<String:String>
类型的Dictionary<String:String>
。 You can't index into a dictionary with string keys using an integer index like that line. 您无法使用像该行这样的整数索引来使用字符串键索引字典。
Your for loop is fetching each key/value pair into a tuple, which is fine, but dictionaries do not have a numeric index. 您的for循环将每个键/值对提取到一个元组中,这很好,但是字典没有数字索引。 In fact, dictionaries are completely unordered. 实际上,字典是完全无序的。 If you want to have a group of questions and answers that you can loop through, use an index to fetch a specific question/answer pair using an index, etc, you should consider an array of tuples, or an array of question/answer structs, or some other data structure. 如果您想要一组可以循环浏览的问题和答案,使用索引使用索引来获取特定的问题/答案对,等等,则应考虑使用元组数组或问题/答案结构数组或其他数据结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.