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

iOS Swift Enums and Structs Structs Stored Properties

What's the difference between enum, struct and class?

Hi all!

I am new to this so please do spend some time helping me. I appreciate that.

Now. I am a java programmer myself. So I do know some basics like interface, class, abstract class etc..

After looking through the videos, I am still pretty confused between enum, struct and class. What is the difference in terms of usage of these and under what kind of scenario will each be suitable?

Thanks!

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Zhen,

I'll try not to bore you too much but hopefully this clears up most of the confusion.

Enums

An enum is considered as a structured data type that can be modified without needing to change say a String or Int multiple times within your code, for example the below shows how easy it would be to change something by accident and forget to change it somewhere else.

let myString = "test"

if myString == "ttest" {
  // Doesn't execute because "ttest" is the value assigned to "myString"
}

With a enum we can avoid this and never have to worry about changing the same thing more than once.

enum MyEnum: String {
  case Test = "test"
}

let enumValue = MyEnum.Test

if enumValue == MyEnum.Test {
  // Will execute because we can reassign the value of "MyEnum.Test" unless we do so within "MyEnum"
}
Structs

I'm not sure how much you know about the MVC pattern but in Swift this is a common practise, before I explain how structs are useful I'll give a quick overview of MVC in Swift.

Model - struct, useful to managing large amounts of data View - Anything that extends UIView, more often than not this is a controller you manage on the storyboard Controller - class, typically used only for views such as UIView controllers and UITableView

Moving on a struct as I said is used for managing large amounts of data, for instance humans are a good example as we can use a struct to manage each person in a contact list.

struct Birthday {
  var day: Int
  var month: Int
  var year: Double
}

struct Person {
  var firstName: String
  var lastName: String
  var birthday: Birthday
  var phoneNumber: String
  var emailAddress: Int
}

For each contact you have you would create a new Person object that contains basic details along with a Birthday struct for complete reusability, the benefit to using the Birthday struct is the fact we can extend it without breaking our code, for example if we needed an easy way to format the persons birthday we can add an additional function without affecting the rest of our code.

Classes

More often than not you would only find classes bound views, when bound to a view iOS will automatically assign a new instance of the class whenever a view is called, the second time the view is called it requests the already created instance of the class.

Other uses for a class is utility helpers which you can create as singletons, this is more of an advanced concept and generally you only need to create code like this on larger applications as generally everything you need is already built-in however it's recommend if you do need additional functionality that you use an extension which allows you to add to any built in object and create your own subscripts

Hopefully that helps.

Hi Chris!

Thanks for your help! :D Have a good 2015 ahead!