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.

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?