Key-Value Observing Programming Guide 閱讀筆記

Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects.A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller.

class 必須首先遵從 NSKeyValueCoding 、然后是 NSKeyValueObserving

Registering for Key-Value Observing

Receiving Notification of a Change

Removing an Object as an Observer

You must perform the following steps to enable an object to receive key-value observing notifications for a KVO-compliant property:

//Register the observer with the observed object
//passing itself as the observer and the key path of the property to be observed. 
//The observer additionally specifies an options parameter and a context pointer to manage aspects of the notifications.
//A safer and more extensible approach is to use the context to ensure notifications you receive are destined for your observer and not a superclass.
- addObserver:forKeyPath:options:context:
//accept change notification messages
- observeValueForKeyPath:ofObject:change:context:
//Unregister the observer,At a minimum, invoke this method before the observer is released from memory.
- removeObserver:forKeyPath:

注意:The key-value observing addObserver:forKeyPath:options:context:method does not maintain strong references to the observing object, the observed objects, or the context.

注冊監(jiān)聽器:

- addObserver:forKeyPath:options:context: 注冊一個監(jiān)聽器用于監(jiān)聽指定Key路徑
- removeObserver:forKeyPath: 為指定Key路徑刪除指定的監(jiān)聽器
- removeObserver:forKeyPath:context: 為指定Key路徑刪除指定的監(jiān)聽器,只是多了一個context參數(shù)。

監(jiān)聽器開始觀察:

- observeValueForKeyPath:ofObject:change:context:

- (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key 

返回yes(默認返回YES),不用調用willChangeValueForKey
返回no斗埂,在改變被觀察的屬性值時盛杰,需要自己調用willChangeValueForKey 或 didChangeValueForKey 方法

如果不使用存取器裁厅,需要在每次修改屬性的實例變量時調用 willChangeValueForKey: 和 調用 didChangeValueForKey:

Key-Value Observing Implementation Details

Automatic key-value observing is implemented using a technique called isa-swizzling.

kvo實現(xiàn)原理圖

The isa pointer, as the name suggests, points to the object's class which maintains a dispatch table. This dispatch table essentially contains pointers to the methods the class implements, among other data.

When an observer is registered for an attribute of an object the isa pointer of the observed object is modified, pointing to an intermediate class rather than at the true class. As a result the value of the isa pointer does not necessarily reflect the actual class of the instance.

You should never rely on the isa pointer to determine class membership. Instead, you should use the class method to determine the class of an object instance.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末搪搏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子堵幽,更是在濱河造成了極大的恐慌,老刑警劉巖弹澎,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件朴下,死亡現(xiàn)場離奇詭異,居然都是意外死亡苦蒿,警方通過查閱死者的電腦和手機桐猬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刽肠,“玉大人溃肪,你說我怎么就攤上這事∫粑澹” “怎么了惫撰?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長躺涝。 經(jīng)常有香客問我厨钻,道長,這世上最難降的妖魔是什么坚嗜? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任夯膀,我火速辦了婚禮,結果婚禮上苍蔬,老公的妹妹穿的比我還像新娘诱建。我一直安慰自己,他們只是感情好碟绑,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布俺猿。 她就那樣靜靜地躺著,像睡著了一般格仲。 火紅的嫁衣襯著肌膚如雪押袍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天凯肋,我揣著相機與錄音谊惭,去河邊找鬼。 笑死,一個胖子當著我的面吹牛圈盔,可吹牛的內容都是我干的豹芯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼药磺,長吁一口氣:“原來是場噩夢啊……” “哼告组!你這毒婦竟也來了?” 一聲冷哼從身側響起癌佩,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤木缝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后围辙,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體我碟,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年姚建,在試婚紗的時候發(fā)現(xiàn)自己被綠了矫俺。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡掸冤,死狀恐怖厘托,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情稿湿,我是刑警寧澤铅匹,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站饺藤,受9級特大地震影響包斑,放射性物質發(fā)生泄漏。R本人自食惡果不足惜涕俗,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一罗丰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧再姑,春花似錦萌抵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至凹联,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間哆档,已是汗流浹背蔽挠。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人澳淑。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓比原,卻偏偏與公主長得像,于是被迫代替她去往敵國和親杠巡。 傳聞我的和親對象是個殘疾皇子量窘,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評論 0 23
  • “一切福田,都離不開心地氢拥“鐾”這句話類似于“吃虧是福”嫩海,意思就是冬殃,在心田上播下善良的種子,總有一天叁怪,它會開花結果审葬。這...
    糖小妹閱讀 324評論 0 0
  • 就像一出戲,有開始也有結束奕谭。我們既是演戲的涣觉,也是看戲的⊙看他人戲里戲外官册,演自己的千秋風云。何人歡呼混驰,誰人在意...
    山貓公子閱讀 138評論 0 2
  • 坊間傳聞“土嗨”最早收錄在殺皇二世強子的火星法典上: ←辷個仒の圡嗨攀隔,兩個仒の徣,⒊天⒊液の放倊燃燒釕漃寞√ 很明...
    一本正經(jīng)得扯淡閱讀 508評論 1 1
  • 回首剛剛過去的2016栖榨,最寶貴的收獲可能在于接觸了投資理財這個領域昆汹。 2016年是進入到現(xiàn)在這個體制內單位的第4年...
    地心閱讀 604評論 0 50