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?

1

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a same thing for struct-only protocol?
@Leem.fin, no you will get an exception if you declare : struct
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.
1

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

2 Comments

Is there a same thing for struct-only protocol?
@Leem.fin Apparently not. Do you have a use case?
0

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

2 Comments

Is there a same thing for struct-only protocol?
@Leem.fin, no, there is not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.