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 and their Methods Review and Final Exam

Hi there! I was wondering when I should use a Struct instead of a Class? I can't really see a difference.

I'm getting really confused about this.

Here is an excerpt from Apple's documentation describing when to use structs vs classes

Choosing Between Classes and Structures

You can use both classes and structures to define custom data types to use as the building blocks of your program’s code.

However, structure instances are always passed by value, and class instances are always passed by reference. This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as a class or as a structure.

As a general guideline, consider creating a structure when one or more of these conditions apply:

  • The structure’s primary purpose is to encapsulate a few relatively simple data values.

-It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.

-Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.

-The structure does not need to inherit properties or behavior from another existing type.

Examples of good candidates for structures include:

The size of a geometric shape, perhaps encapsulating a width property and a height property, both of type Double.

A way to refer to ranges within a series, perhaps encapsulating a start property and a length property, both of type Int.

A point in a 3D coordinate system, perhaps encapsulating x, y and z properties, each of type Double. In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.

Please ask me if there is anything you don't understand in the above excerpt, or if you would like me to try and explain it in a different way.

Lauren Hibbs
Lauren Hibbs
1,872 Points

Here is a little example that I created when learning the difference between a struct and a class. Basically, when you change something in/about a class, all past references to that class will be changed. However, because a struct is a value type, references to a struct only hold a value, and not an actual reference to the struct object. This means that when a struct is changed, past references will still hold the same value. However, when a class is changed, all references to that class with be affected.

struct School {
    var name : String
    var students: [Student] = []

    init(schoolName : String){
        students = []
        name=schoolName
    }

    mutating func addStudent(student : Student){
        students.append(student)
    }

    mutating func changeName(confusing : String){
        name = confusing
    }
}

struct Student{
    var name :String
    var aSchool :School

    mutating func enroll(){
        aSchool.addStudent(self)
    }

    func currentSchool ()-> String
    {
        return aSchool.name
    }

    mutating func changeSchool(newSchool : School)
    {
        aSchool = newSchool
    }

}

var mySchool = School(schoolName:"Random School")
var otherSchool = School(schoolName:"Treehouse")
var person = Student(name:"Lauren", aSchool:mySchool)
person.enroll()
person.currentSchool()   // school is Random School

person.changeSchool(otherSchool)
person.currentSchool()  // school is Treehouse

otherSchool.changeName("Code School")
person.currentSchool()  // school is still Treehouse!!

If you notice on the last line, because School is a struct, when the name is changed the student object still holds the other value of school. If school was a class the .currentSchool would return CodeSchool. Hope this helps!