0

Since I don't know what the name is for the following thing, I don't know how to use proper words to describe my question. ANyway, I am wondering what is the :class ? What does it mean? and what does it do?

protocol MyDelegate: class {
    func doTask()
}

==== update ===

OK, I got the answer, thanks guys, but it would be nice not only tell it is class-only protocol. But also what about enum & struct only protocol, because naturally this question will raise up, is there a way to define enum-only and struct-only protocol then?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

3 Answers3

2

class in your case means that classes only can implement your protocol and not structs.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Is there a same thing for struct-only protocol? – Leem.fin Sep 06 '17 at 09:19
  • @Leem.fin, no you will get an exception if you declare `: struct` – Rashwan L Sep 06 '17 at 09:23
  • 2
    Just to give some context, `enum`, `struct` and `tuple` are value types. `class` is the only reference type. Value types represent a value (e.g. ServerStatus) and reference represent an identity (e.g. Person). Swift treats them very differently, Value types are copied when assigned to another variable, where as reference types pass the reference to that instance. When you have a variable of protocol type and you want to create it as a weak reference, you would need the protocol to be defined as `: class`. That is just one example. Apple's iBooks for Swift would be helpful. – user1046037 Sep 06 '17 at 09:53
1

That means that only classes (not structs) can implement it.

Wukerplank
  • 4,156
  • 2
  • 28
  • 45
0

It means that you can conform to this protocol only classes, not structures or enums.

Vlad Khambir
  • 4,313
  • 1
  • 17
  • 25