How to define and implement properties in protocol

前端 未结 2 1198
庸人自扰
庸人自扰 2020-12-28 18:05

I want to define one protocol with few properties and need to use those properties in another NSObject subclass. Please give me link or example code. I need that to work wit

2条回答
  •  渐次进展
    2020-12-28 18:18

    @property just says to the compiler that the class is expected to define the methods to match that property.

    @protocol MyProtocol
    @property (nonatomic, readonly) id someObject;
    @property (nonatomic, getter=isAlive) BOOL alive;
    @end
    

    Anything implementing that protocol will now need to have

    - (id)someObject;
    - (BOOL)isAlive;
    - (void)setAlive:(BOOL)aBOOL;
    

提交回复
热议问题