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
Ryan Maneo
4,342 PointsA few questions about protocols
Can get be used elsewhere, or only in protocols? e.g can I define { get } inside of a class to say that it is gettable, or is it conformed only to protocol implementations?
Wait what? Can protocols be used for all custom types? Or other things? Are they made for custom types like Enums are made for Optionals? Or do they have many other uses?
What exactly does... { get } do? I don't understand what gettable means... isn't it already 'gettable'?
1 Answer
Connor Crawford
6,405 PointsLet me see if I can clarify these questions for you. You can make all kinds of things gettable or settable or both, it just depends on your implementation and what you are trying to do. A property that is set to get is retrieving information while a property that is being set allows you to change what the information will be. Protocols are very very important as it saves you a lot of code and allows you to write flexible code and one of their main functions is to be used as a delegate (read up on these) as I am still learning myself. They can be used as a type themselves or to make another type conform to its structure making them really powerful. Anyways back to { get }, this means you can get the property and use it somewhere else in your code. { Set } means you can set it somewhere else in your code, where using both allows you to do both somewhere else in your code then the actually property itself is defined.