How to conform to a self-made protocol?

后端 未结 7 1761
忘了有多久
忘了有多久 2021-02-10 16:33

I have a class with an delegate property. Anyone who wants to be a delegate must conform to a protocol. I defined everything like this:

#import 

        
7条回答
  •  粉色の甜心
    2021-02-10 17:19

    Your two classes differ in their use of the protocol. MyClass does not implement the protocol, it has an attibute that is a pointer to a class that implements the protocol. OtherClass should implement the protocol.

    OtherClass needs to have available before its interface is defined all the details of the protocols, interfaces and classes that it inherits from. Thus you need the protocol in a header to be #imported in OtherClass.h

    MyClass just needs to know the protocol exists in its interface.

    Note on Stephen's reply subclassing is the case you can't use forward declarations of classes. (In the example OtherClass is a subclass of NSObject)

提交回复
热议问题