Tanguy MP
Tanguy MP

Reputation: 159

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). 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. Shouldn't I be able to access my dictionary by index.

Upvotes: 1

Views: 93

Answers (3)

Duncan C
Duncan C

Reputation: 131408

Your listOfQuestionsAndAnswers is a dictionary of type 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. 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.

Upvotes: 0

Lucho
Lucho

Reputation: 1044

listOfQuestionsAndAnswers is not an array is a Dictionary and listOfQuestionsAndAnswers[someIntIndex] is not working because your keys are strings

    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
            }
        } }

Upvotes: 0

JAL
JAL

Reputation: 42449

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.

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"

Upvotes: 1

Related Questions