Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dennis Parussini
Treehouse Project ReviewerHelp with Swift Enum and it's instances!!!
Hi,
my question is as follows:
Do I need to assign a member value to an enum every time I create an instance of it? I think I have to, but want to make sure. In a Playground I created the following Enum and a designated initializer.
enum Names {
case Tom, Jenny
init() {
self.init()
}
}
When I create an instance of the Names enum I would think that the following should work:
var name = Names()
It doesn't though. Instead I need to explicitly assign a member value to the enum to use it properly:
var name = Names.Tom
Again, the question is if I really need to do it this way, or did I forget something, that was mentioned in the course videos?
Dennis Parussini
Treehouse Project ReviewerIt shouldn't actually have a value at all. All I would like to do is to create an instance of the enum without giving it an initial value. I was thinking about this because you can do so with structs and classes.
miguelcastro2
Courses Plus Student 6,573 PointsEnums are just ways of organizing constants and in many cases you do not require any instances of them. I believe if you want the Enum to default to a value then you can set that value in your init() method, otherwise you would just reference the Enum statically. So if you wanted your instance to default to Tom it could look like this:
init() {
self = .Tom
}
Dennis Parussini
Treehouse Project ReviewerOk, thanks for the info. That helped me a lot. :-)
miguelcastro2
Courses Plus Student 6,573 Pointsmiguelcastro2
Courses Plus Student 6,573 PointsLooking at this code, what do you expect your name variable to return? What value should it provide?