2017-05-30 6 views
0

Ich mache ein mathematisches Quizspiel. In dem Spiel wählt der Benutzer eine Einheit aus und dann stellt das Spiel Fragen über Übungen dieser Einheit.Verschiedene Klassen

Die Klassen I haben, sind:

  • Viewcontroller

  • UnitSelector - ist verantwortlich für das Gerät informieren, dass der Benutzer

  • Unit01 ausgewählt hat ... bis ... Einheit 20 - sind verantwortlich für die Rückkehr nach dem Zufallsprinzip eine Ausübung der Einheit

Und viele Übungen

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

Das Problem ist, dass ich weiß nicht, wie diese Situation zu lösen: Ive es aufgeteilt in Einheiten und jede Einheit ihre eigene Übungen hat. Ich habe ungefähr 20 Einheiten und in jeder Einheit ungefähr 5 Übungen. In der Steuerung ist der Typ der unitClass Any und ich muss die Klasse haben, die UnitSelector.getUnitClass zurückgibt, Unit01() ... Unit20(). Ich weiß nicht, ob die Logik, die ich befolgte, richtig ist, also wenn mir jemand helfen kann ...

Danke !!

+0

Vielleicht fügen Sie eine Schließeigenschaft 'exercice' in' Unit' ein und legen Sie sie beim Initialisieren fest. – shallowThought

Antwort

0

Ihre Frage ist für mich nicht ganz klar, aber ich versuche zu helfen: Sie können den Typ der Klasse erhalten:

type(of: yourObject) 

in diesem Beitrag erwähnt: How do you find out the type of an object (in Swift)?

Haben Sie einen viel (meiner Meinung nach) bessere Lösung für dieses Problem. Einer ist ein Array-basierter Ansatz:

//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) 

Ich hoffe, dies wird hilfreich sein!

Verwandte Themen