Send Notification When a Property is Changed Using KVO

后端 未结 3 786
独厮守ぢ
独厮守ぢ 2021-02-19 05:11

I had a property named myName in my class, like:

@property (nonatomic, strong) NSString *myName;

I need to send a notification whe

3条回答
  •  再見小時候
    2021-02-19 05:42

    To do this without the customer setter, just synthesize the property setter. This will create all the supporting calls to willChangeValueForKey / didChangeValueForKey.

    @synthesize myName;

    Then set property values with dot-syntax:

    self.myName = @"Inigo Montoya"

    Then the observers will receive the KVO notification automatically.

    (You will need to remove the observer before you release the observed object.)

提交回复
热议问题