Dave
Dave

Reputation: 5

Brief property's getter description in Swift

I need your help. Can you please describe me what means writing { get } in this statement:

public var isLandscape: Bool { get }

I mean it is quite easy to understand this is a read-only computed property but what I don't understand is why braces is not put after get word because if I want to create my own computed property like that I should put them and describe its return value? Does it mean that Apple has wanted to hide implementation of that getter and show property's read-only type and nothing more?

Upvotes: 0

Views: 61

Answers (2)

Slayter
Slayter

Reputation: 1170

This form of declaring a property is usually used when declaring a protocol. I've also seen it used in generated interfaces.

In the case of a protocol it denotes that the conforming object should expose a getter for the property however a property with both get and set methods will work.

Upvotes: 1

theMikeSwan
theMikeSwan

Reputation: 4729

This is how property declarations look in protocols and in the generated interface files for Swift classes in Xcode.

To see this select one of your own Swift class files and open the assistant editor. Select the option for yourClassName(interface) from Counterparts in the assistant editor and you will see the same kind of property declarations.

All of the Apple class files are presented as interfaces in Xcode so we aren't bogged down with implementation details.

Upvotes: 0

Related Questions