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 Build a Weather App with Swift (Retired) Data Modeling With Structures Using Structures to Model Data

David Fry
David Fry
412 Points

Why use a struct and not a class to hold the data?

Why use a struct and not a calls?

Hey David,

The course instructor might have specific reasons I'm not aware of, but this might be helpful. http://stackoverflow.com/questions/24217586/structure-vs-class-in-swift-language

4 Answers

Felix Salazar
Felix Salazar
3,879 Points

No, if you want to have an object with updated values, you should use a class, because (as the definitions tells) classes are passed by reference, which means: is the same object in all ambits. This link have an example.

Felix Salazar
Felix Salazar
3,879 Points

There are some differences between them; You can see them here.

Long story short: Struct are more simple (or less complex, as you want to see them) than Classes. So it's a matter of choice.

David Fry
David Fry
412 Points

So because we are going to be updating the value we use a struct because it is passed by copy? So lets say we have multiple locations would you create a new struct for each. IE: var currentLocation = Currently() and then var newLocation = Currently(). How is that different than creating classes?

Felix- What is the best one to use in a productions app?

The link shared by Matt has the answer to your question. http://stackoverflow.com/questions/24217586/structure-vs-class-in-swift-language