# Enum associated values with validation

**URL:** <https://forums.kodeco.com/t/enum-associated-values-with-validation/31730>\
**Category:** Swift\
**Created:** [July 4, 2017, 1:51am UTC](https://forums.kodeco.com/t/enum-associated-values-with-validation/31730 "2017-07-04T01:51:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagmeet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jagmeet/32/68880_2.png) [@jagmeet](https://forums.kodeco.com/u/jagmeet)\
**Post date:** [July 4, 2017, 1:51am UTC](https://forums.kodeco.com/t/enum-associated-values-with-validation/31730/1 "2017-07-04T01:51:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jagmeet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jagmeet/32/68880_2.png) [@jagmeet](https://forums.kodeco.com/u/jagmeet)\
**Post date:** [July 4, 2017, 3:35am UTC](https://forums.kodeco.com/t/enum-associated-values-with-validation/31730/2 "2017-07-04T03:35:56Z")

</div>

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.
