Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 不同类别_Swift_Class_Types_Switch Statement_Return - Fatal编程技术网

Swift 不同类别

Swift 不同类别,swift,class,types,switch-statement,return,Swift,Class,Types,Switch Statement,Return,我正在做一个数学测验游戏。在游戏中,用户选择一个单位,然后游戏询问有关该单位练习的问题 我的课程有: 视图控制器 UnitSelector-负责通知装置用户 选择 单元01…到…单元20-负责随机返回一个 该股的运作 还有很多练习 class UnitSelector { var unit: Int! init(unit: Int) { self.unit = unit } func getUnitClass() ->

我正在做一个数学测验游戏。在游戏中,用户选择一个单位,然后游戏询问有关该单位练习的问题

我的课程有:

  • 视图控制器

  • UnitSelector-负责通知装置用户 选择

  • 单元01…到…单元20-负责随机返回一个 该股的运作

还有很多练习

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"
  }
}
问题是我不知道如何解决这种情况: 我把它分成几个单元,每个单元都有自己的练习。我将有大约20个单元,每个单元大约有5个练习。在控制器中,unitClass的类型是Any,我需要UnitSelector.getUnitClass返回的类,Unit01()…Unit20()。 我不知道我遵循的逻辑是否正确,所以如果有人能帮助我


谢谢

你的问题我不太清楚,但我想帮你: 您可以通过以下方式获取类的类型:

type(of: yourObject)
在这篇文章中提到:

我认为有很多更好的办法来解决这个问题。一种是基于阵列的方法:

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

我希望这将是有益的

可以在
单元
中引入闭包属性
exercice
,并在初始化时设置它。