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 Protocols in Swift Protocol Basics Creating a Protocol

Kanish Chhabra
Kanish Chhabra
2,151 Points

what are gettable and settable properties?

what are gettable and settable properties?

2 Answers

Gettable properties are values that you can read.

Settable properties are values that you can edit, but not read.

If you have a property that's both gettable and settable, you can read and edit the value.

Kanish Chhabra
Kanish Chhabra
2,151 Points

protocol User { var name: String { get } var age: Int { get set } }

struct Person: User { var name: String var age: Int }

var somePerson = Person(name: "Rohan", age: 16) somePerson.name = "Smith"

let blaBla: String = somePerson.name + " bla bla"

Here I have edited the gettable property, how does this relate with 'read only'?

What do you mean?

Gavin Hobbs
Gavin Hobbs
5,205 Points

Hey Kanish Chhabra here are my thoughts on your code (I included my thoughts as comments in the code)...

protocol User {
    // Variables!
    var name: String { get }
    var age: Int { get set }
}

struct Person: User {
    // And more variables, notice these aren't constants
    var name: String
    var age: Int
}

// You are setting the properties for the protocol here (i.e. name and age).
//Also, notice that this is a variable you're setting this struct/protocol to
var somePerson = Person(name: "Rohan", age: 16)

// Here, you're changing the variable: name, in the variable/object: somePerson, :p which is entirely possible
somePerson.name = "Smith"
// The struct was a blueprint and the protocols were the necessary supplies in the blueprints...
// Now that you've built what the blueprint called for you can change it to however you like it...
// Because it isn't welded together (a constant), it's screwed together (a variable) 

let blaBla: String = somePerson.name + " bla bla"

Hope this helps!

G'day!

-Gavin