0

I´m making a mathematical quiz game. In the game the user selects a unit and then the game ask questions about exercises of this unit.

The classes I have are:

  • ViewController

  • UnitSelector - is responsible for inform the unit that the user has selected

  • Unit01…to…Unit20 - are responsible for return randomly one exercise of the unit

And lots of exercises

class UnitSelector {

      var unit: Int!

      init(unit: Int) {
        self.unit = unit
      }

      func getUnitClass() -> Any? {
        switch unit {
        case 0:
          return Unit01()
        case 1:
          return Unit02()
        // all the other cases
        case 19:
          return Unit20()
        default:
          return nil
        }
      }
    }

class GameViewController: UIViewController {

  override func viewDidLoad() {
    // more code
    unitSelector = UnitSelector(unit: selectedUnit)
    unitClass = unitSelector.getUnitClass()
    let question = unitClass.giveMeQuestion()
  }

  // all the other code
}

// all the Units are like this one
class UnitXX {
  // vars

  func giveMeQuestion() -> String {
    // code

    return "Question"
  }
}

The problem is that I don´t know how to solve this situation: I´ve divided it in Units and each unit has its own exercises. I´ll have about 20 Units and in each unit about 5 exercises. In the controller the unitClass´s type is Any and I need to have the class that the UnitSelector.getUnitClass returns, Unit01()...Unit20(). I don´t know if the logic that I followed is the correct, so if someone can help me...

Thanks!!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Idalaia
  • 1
  • 1
  • 3

1 Answers1

0

Your question is not totally clear for me, but I try to help: You can get the type of the class by:

type(of: yourObject)

mentioned in this post: How do you find out the type of an object (in Swift)?

Have a lot of (in my opinion) better solution for that problem. One is an array-based approach:

//questions is an array with other array inside
var questions: [[String]] =
    [
        ["Question 1 for unit type One",
         "Question 2 for unit type One",
         "Question 3 for unit type One",
         "Question N for unit type One"],

        ["Question 1 for unit type Two",
         "Question 2 for unit type Two",
         "Question 3 for unit type Two",
         "Question 4 for unit type Two",
         "Question N for unit type Two"],

        ["Question 1 for unit type N",
         "Question 2 for unit type N",
         "Question N for unit type N"]
    ]

//getting random number between 0 and the count of the questions "outter" array
var randomUnitNumber = Int(arc4random_uniform(UInt32(questions.count)))

//getting the "inner" array with questions
var questionsForUnit = questions[randomUnitNumber]

//getting random number between 0 and the count of the questions "inner" array
var randomQuestionNumber = Int(arc4random_uniform(UInt32(questionsForUnit.count)))

//getting the question
var randomQuestion = questionsForUnit[randomQuestionNumber]

//printing the question
print(randomQuestion)

I hope this will be helpful!