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"