David Liu
David Liu

Reputation: 17584

Why always prefix type property requirement with the static keyword in Swift protocol?

On page 369 of the book The Swift Programming Language, it says “Always prefix type property requirements with the static keyword when you define them in a protocol.”

Example Code:

protocol AnotherProtocol {
    static var someTypeProperty: Int { get set }
}

What is the reason or benefit of doing so?

Upvotes: 5

Views: 727

Answers (1)

BoltClock
BoltClock

Reputation: 723388

Because without the static keyword you end up declaring an instance property rather than a type property. The example that immediately follows the paragraph you quote shows this:

Here’s an example of a protocol with a single instance property requirement:

protocol FullyNamed {
    var fullName: String { get }
}

Upvotes: 4

Related Questions