When to use self on class properties?

前端 未结 8 1342
暗喜
暗喜 2020-12-03 09:03

When is self needed for class properties? For example:

self.MyProperty = @\"hi there\";

vs

MyProperty = @\"hi there\";
         


        
8条回答
  •  清歌不尽
    2020-12-03 09:42

    If you use automatic Key-Value Observing (or any Cocoa technology that builds on it - like bindings, ...), it is also important use the setter. The observer would not receive any notification if you assign to the ivar. If you bind "MyProperty" to a NSTextfield and you change your "MyProperty" ivar via code, the bound textfield would still display the old value as it did not receive any change notification.

提交回复
热议问题