Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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,019 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.