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
Vukasin Pavlovic
8,108 PointsHow to know when to user classes, and when to use structs?
Actually, i dont know when exactly to use classes or structs, so can you give me some example of how i need to use them?
2 Answers
jcorum
71,830 PointsHere's a web page with an interesting take: https://www.mikeash.com/pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html
There's a section in the Apple documentation on when to use which: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html
You will need to scroll down to the Choosing Between Classes and Structures section.
Reed Carson
8,306 PointsA general answer
Use structs when you need a value type, and use classes when you need a reference type. and use classes if you think you'll want to use inheritance.