Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.