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

Moises Miguel
Moises Miguel
191 Points

Are structures in swift the same as objects in javascript?

I am comming from an extensive background in javascript(Angularjs/React.js) and I am now learning swift. I see that structures are closely related to objects in javascript.

My question is .. Are structures (struct) the same as objects in javascript and if not what advantages does one have over the other?

Gillian Reynolds-Titko
Gillian Reynolds-Titko
6,021 Points

Moises, I'll get this started and hopefully others can fill in. A struct in Swift is a complex data type that can have variables and methods associated with it. You can create instances of the struct (like you would create instances of your JS object). A struct is also a 'value type' . This means that if you copy a struct, when you make a change to one copy, it does not affect the other copy (this behavior is unlike that of a class, which is a reference type - when you copy an instance of a class (i.e. create an object) both copies reference the same data). A struct also does not have inheritance like a class would. I'm not very familiar with JS to say if the object in JS is acting like a class in Swift (i.e. is a reference type). Hopefully others can expand on this information.