0

I have a Computer Science ISP to create a trivia game. I am trying to set a title of a button from an item in a Multi-Dimensional array. The array consists of 22 questions, type String arrays with 6 items in the array. Looking like this :

let Q1:[String] = ["3","Who invented the telephone?","Rogers","Virgin Mobile","Graham Bell","Nikola Tesla","0"]

I have a shuffled Array that includes all 22 questions(22 Q1-Q22's) and I want a button's title to be the second item in the first array in the shuffledArray. But it gives me this error:

Cannot convert value of type 'Any' to expected argument type 'String?'

enter image description here

6
  • Could you add the declaration of shuffledArray? Commented Jan 19, 2018 at 18:12
  • Tried putting “as! String” at the end? Commented Jan 19, 2018 at 18:12
  • String(describing: shuffledArray[0][2]) should work too. Exactly as you did for qLabel Commented Jan 19, 2018 at 18:18
  • @GIJOW Never use String(describing:) for user-facing content. Ever. Commented Jan 19, 2018 at 18:49
  • @Alexander can you help me understand why ? Didn’t find any reference avoidance on apple docs. Thanks Commented Jan 20, 2018 at 11:06

1 Answer 1

1

Based on the compile error you are getting, I would assume that if you tried to press option and click on shuffledArray, probably, it would be of type [[Any]] (depends on how you are declaring it). If you know that the element should be a String, you could cast it as follows:

if let qLabelText = shuffledArray[0][1] as? String {
      qlabel.text = qLabelText
}

But

I would note that:

  • if you are pretty sure that shuffledArray should be an array of strings array, then you should -somehow- declare it [[String]] instead, thus there is no need to cast the element as string.

  • However, for such a case, creating your own custom Model for such a case (template) might be a better choice of doing it as a 2D array.

For instance:

struct QuestionModel {
    var number: String
    var content: String

    var answer1: String
    var answer2: String
    var answer3: String
    var answer4: String

    var correctAnswer: String
}

Definitely, the structure of declaring the model is based on your requirement and may not be necessary to clone it as is.

You could declare your question as:

let question1 = QuestionModel(number: "3", content: "Who invented the telephone?", answer1: "Rogers", answer2: "Virgin Mobile", answer3: "Graham Bell", answer4: "Nikola Tesla", correctAnswer: "0")

Thus shuffledArray would be an array of QuestionModel ([QuestionModel]) instead of [[String]].

Now, you could write your newQ function as:

func newQ() {
    let queestion1 = shuffledArray[0]

    qlabel.text = question1.content
    button1.setTitle(question1.answer1, for: .normal)
    button2.setTitle(question1.answer2, for: .normal)
    button3.setTitle(question1.answer3, for: .normal)
    button4.setTitle(question1.answer4, for: .normal)
}

Following the above approach would be more expressive and easy to work with.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.