Let’s say I have an enum with associated values:
enum Quiz {
case numbered(questions: Int)
case timed(seconds: Int)
}
I want to make sure that case numbered accepts only the associated values of [10, 20, 30, 40, 50] and timed only [30, 60, 90, 120, 180]. In all other cases, it should fail initialization.
How would I do that?
Never mind! I did it as nested enums instead, like below:
enum Quiz {
case numbered(questions: NumberedQuestions)
case timed(seconds: TimedSeconds)
case worksheet(questions: WorkSheetQuestions)
//Define permissible number of questions for numbered quiz
enum NumberedQuestions: Int { case _10 = 10; case _20 = 20; case _30 = 30; case _40 = 40; case _50 = 50; }
//Define permissible time in seconds for timed quiz
enum TimedSeconds: Int { case _30 = 30; case _60 = 60; case _90 = 90; case _120 = 120; case _180 = 180}
//Define permissible number of questions on a worksheet
enum WorkSheetQuestions: Int { case _10 = 10; case _20 = 20; }
}
If someone has a better idea, I’m all ears.