What's the difference in leaving a variable in the protocol marked static or non-static in Swift 4?

1

I'm learning about protocol s now, and I saw a code snippet in Swift 4 that defines a protocol, below the snippet of code.

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

Note that the variable someTypeProperty is declared as static , and in this case below it appears without static .

protocol AnotherProcol {
  var someTypeProperty: Int { get set }
}

What's the difference between these two types of protocol implementation in Swift 4 language?

    
asked by anonymous 11.08.2018 / 22:11

1 answer

1

The specific question for the protocol seems to indicate already know why static is used in a regular type, such as a class, for example. The reason is the same, it just differs that the protocol is just the indication of a contract.

A static indicates that the member is a type and not an instance. Used in protocol it indicates that any type that decides to implement this specific protocol, in the example AnotherProcol , must have a property called someTypeProperty of type Int with the methods accessor and < in> mutator . The protocol does not day anything else as they should be, in the concrete type they can have a code implementation if it is desired, the protocol only requires that the property exists, does not say how it should proceed, whether it should have some auxiliary field, nothing.

    
11.08.2018 / 22:58