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 Creating Flexible Objects Using Protocols Protocol Inheritance

niilo pirttijarvi
niilo pirttijarvi
1,232 Points

Protocols ins swift task. Var vs Let?

Why should the cuddlyName be a variable instead of a constant?

protocols.swift
protocol Animal {
    var numberOfLegs: Int { get }
}
protocol Pet: Animal {
    let cuddlyName: String { get }
}

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Niilo -

Taken from Swift documentation

If a protocol requires a property to be gettable and settable, that property requirement can’t be fulfilled by a constant stored property or a read-only computed property. If the protocol only requires a property to be gettable, the requirement can be satisfied by any kind of property, and it’s valid for the property to be also settable if this is useful for your own code.