1
I’m learning about protocol
s now, and I saw a code snippet in Swift 4 that defines a protocol, follows below the code snippet.
protocol AnotherProtocol {
static var someTypeProperty: Int { get set }
}
Note that the variable someTypeProperty
is stated as static
, and in this other case below it appears without the static
.
protocol AnotherProcol {
var someTypeProperty: Int { get set }
}
What is the difference between these two types of protocol implementation in the Swift 4 language?
I didn’t quite understand it... so Static is useless ? Just to indicate that the property has to have get and set ? but this is not indicated right after the var variable:Int {get set} ?
– Victor OCV
Like I said, you know you need to know what the
static
out of theprotocol
, you know? If you don’t know it is better to understand it first and then try to understandprotocol
. When you try to learn things out of order it is harder to learn anything, which is why I always recommend people go down a structured path. Picking up random things never worked.– Maniero
I know what Static is in the concept of c#.. example when you define a method as Static it can be accessed without an instance of the class.
– Victor OCV
Okay, I’ll take it you do, you know what the interface is on C#?
– Maniero
Yes, I’ve implemented interfaces in c#
– Victor OCV
So knowing what the
static
and what isinterface
of C#, that’s exactly it, only in Swift the interlude she calls the accepted protocolstatic
, as C# 8 will also accept.– Maniero
It is if you are to think, protocols have the same sense of interface... Make requirements of methods and attributes that should be implemented for those who adopt.
– Victor OCV
Exactly what I said in the reply.
– Maniero
You iOS program ?
– Victor OCV
I never created anything.
– Maniero