Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use class only protocols in Swift?

Can anyone please explain class only protocols to me in Swift. I understand what protocols are and why we use them. I also understand that its recommended to use class only protocols when we use reference type objects in it and want to limit the protocol conformation to classes only. However, I can't find any good answer to support that recommendation. Why is it recommended? What is the drawbacks of using normal protocols in that case.

like image 593
Vik Singh Avatar asked Dec 20 '22 08:12

Vik Singh


1 Answers

One use case:

  • You have a "delegate" protocol and someone wants to have a weak property of that protocol type. weak can only be used on reference types; therefore, the protocol must be class-only.
like image 84
user102008 Avatar answered Dec 24 '22 00:12

user102008