ios - 在看KVO和category的时候看到一段文字,应该怎么样理解?
阿神
阿神 2017-04-17 14:29:56
0
2
408

When using KVO in a category implementation, it is recommended that a custom associated-object be used as an observer, rather than the object observing itself.

这个要怎么理解?

阿神
阿神

闭关修行中......

reply all(2)
大家讲道理

KVO generally monitors certain attribute changes of an OBJ_A and provides callbacks if expected changes occur.
Observing it yourself is theoretically OK (I haven't tried it...), because to put it bluntly, KVO is to observe the SETTER method of a certain attribute, but the readability of the code written this way is poor. The method everyone adopts is to write a separate associated object. .

The advantages of writing alone are:

  • Highly readable, don’t worry about observing yourself
  • Decoupling
  • It is convenient for business expansion, because the associated object is your custom object after all (it can be very abstract)

BLABLA

KVO, the interface provided by Apple, is too fucked up. It was designed with a very good idea. . . If you want to use KVO, I recommend Facebook’s KVOController

大家讲道理

My personal feeling is that in order to keep the category simple, writing categories is generally relatively basic, but when you add KVO, you embed the business logic, which makes the possibility of this code being reused in the future

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template