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 Object-Oriented Swift Value vs Reference Types Structs vs Classes

Ryan Jin
Ryan Jin
15,337 Points

Difference between Struct and Class

Is all the difference between Struct and Class is that a copy of an instance of class is the same as the original instance, and in struct, it creates a new object?

1 Answer

James Marchant
James Marchant
6,963 Points

A class is passed by reference and a struct is passed by value. So yes, the struct is copied to the new value where as the class is just passing the reference to the object over to the new value, So that value a and value b both 'point' or reference the same object.

Here is a great thread that helped me understand it better on stackOverflow: http://stackoverflow.com/questions/24217586/structure-vs-class-in-swift-language

Ryan Jin
Ryan Jin
15,337 Points

Is that the only difference?