Home  >  Q&A  >  body text

ios - 在看KVO和category的时候看到一段文字,应该怎么样理解?

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.

这个要怎么理解?

阿神阿神2716 days ago355

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 14:31:56

    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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 14:31:56

    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

    reply
    0
  • Cancelreply