Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 879
David Liu Avatar asked Sep 28 '22 10:09

David Liu


1 Answers

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 }
}
like image 187
BoltClock Avatar answered Oct 12 '22 23:10

BoltClock