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


Alfredo Elizondo
Alfredo Elizondo
1,583 Points

Can i have an enum made of several Objects?

I have the following class

class Car{
   var doors:Int

   func accelerate() {
     //Speed increase function

// Can i have an enum of cars? 

enum cars { 
case .honda, .mercedes // Etc... 

and each one of those still have the property doors or function accelerate?

Should i use an array on that case?

2 Answers

No, sorry. Swift supports only 4 types for the value of an enum: Integer Floating Point String Boolean

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Alfredo,

Just to expand a bit on jcorum's answer:

Enums are a type definition in and of themselves, like Classes and Structs, so it doesn't really make sense to think about making an enum of some type. However, enums can have associated values. Those values can only be of type Integer, Float, String or Bool.

In the use case you're asking about, an enum doesn't really make sense, since enums should typically describe a finite set of data (you would need to list out every type of car in existence). Instead, you should create instances of Car, and store them in variables named honda, mercedes, etc. Then, depending on how you plan to use them, you could store them in an array or a dictionary.