Implement own setter or use KVO?

后端 未结 3 1061
臣服心动
臣服心动 2021-02-07 22:22

In short, when the property value changing, I have to update some logic in my code, for example:

- (void)setProp:(NSString *)theProp
{
  if (prop != theProp){
           


        
3条回答
  •  死守一世寂寞
    2021-02-07 23:13

    In your case the best option is to add the logic inside the setter. Your setter implementation is correct if your property declaration is something like

    @property (nonatomic, copy) YCYourClass *prop;

    KVO is used normally when checking for changes on external objects properties.

    NSNotifications are better suited to inform of events.

提交回复
热议问题