In Swift, if I create a delegate protocol, it can be negotiated using the class and structure.
protocol MyDelegate { // Can be conformed to by class or struct }
The problem arises when I declare a delegate. If the delegate is an instance of the class, I want the variable to be weak in order to avoid saving the loop. If this is a structure, there is no such need - in fact, Swift will not allow me to make the delegate variable weak. Note. I know how to create a weak delegate, but the key question is: if you create a delegate protocol that can be weak, unless you make it compatible only with the class, you cannot enforce the persistence loop.
class MyClass {
It seems we need to declare that the protocol will be for the class only at all times, for example, because a non-regular delegate protocol cannot prevent save cycles:
protocol MyDelegate: class { }
The fact that Swift allows you to shoot in the foot in this way seems to contradict his design philosophy in safety.
source share