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 trialerik Jones Oniga
3,307 Pointsget set
Hi,
can somebody explain me easily, what structs and protocols are and where they are used(maybe some examples). I just don't understand what get and set are, and do they have always to be used in a protocol? Woule appreciate it, if someone wozld explain me these things.
Thanks!
1 Answer
Clark Reilly
6,204 PointsPasan does come back to the topic later in the course, but the important points are (afaik):
- structs are a custom data type (like a String or an Int) that represents something that doesn't change. For example something like a map. No matter where you go, the map still looks the same. The difference between structs and classes mostly has to do with being value and reference types respectively. More info: net-informations.com, swift documentation
- protocols are rather complicated, but the gist of it is that they don't really serve any purpose beyond convenience and save debugging time. They force your classes/structs to implement the variables, constants and/or functions they contain so you don't forget them and help other coders follow your work when you're working in a team. More info: swift documentation
- get / set just marks whether the stored property should be a constant or a variable. Constants are get only, they can't be set to something else. Variables are gettable and settable: You can get the value and you can change it. If you don't use them, the protocol can't figure out which it's supposed to be. All of this makes more sense as well once you cover computed properties. More info: stackoverflow