Int不能转换为Dictionary< string,string> [英] Int is not convertible to Dictionary<string, string>

查看:122
本文介绍了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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆