Int不能转换为Dictionary< string,string>

编程入门 行业动态 更新时间:2024-10-10 19:26:25
本文介绍了Int不能转换为Dictionary< string,string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此行出现错误(questionField.text = listOfQuestionsAndAnswers [currentQuestionIndex])(Int不能转换为Dictionary)。 此外,我想让所有问题逐一显示,最后一个问题之后,谁是保罗应该再次显示...

There is an error at this line (questionField.text = listOfQuestionsAndAnswers[currentQuestionIndex]) (Int is not convertible to 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 } } }

我得到了错误Int不能转换为DictionaryIndex,我不明白这是什么意思。我不能通过索引访问我的字典。

I am getting the error Int is not convertible to DictionaryIndex and I don't understand what that means. Shouldn't I be able to access my dictionary by index.

推荐答案

你不能用诠释。词典包含键和值,并由键下标。在这种情况下,您的 listOfQuestionsAndAnswers 是一个字典,键和值都是字符串。

You can't subscript a dictionary by 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.

如果要下标通过Int,考虑使用(String,String)元组的数组。

If you wanted to subscript by Int, consider using an array of (String, String) tuples.

如果要使用字典,您必须通过其键值从字典获取值:

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"

更多推荐

Int不能转换为Dictionary&lt; string,string&gt;

本文发布于:2023-11-12 13:00:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581566.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   Dictionary   Int   amp   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!