Selectors or Blocks for callbacks in an Objective-C library

后端 未结 4 1176
庸人自扰
庸人自扰 2021-02-02 12:35

Question

We\'re developing a custom EventEmitter inspired message system in Objective-C. For listeners to provide callbacks, should we require blocks or selectors and

4条回答
  •  天涯浪人
    2021-02-02 13:27

    Great writeup!

    Coming from writing lots of JavaScript, event-driven programming feels way cleaner than having delegates back and forth, in my personal opinion.

    Regarding the memory-managing aspect of listeners, my attempt at solving this (drawing heavily from Mike Ash's MAKVONotificationCenter), swizzles both the caller and emitter's dealloc implementation (as seen here) in order to safely remove listeners in both ways.

    I'm not entirely sure how safe this approach is, but the idea is to try it 'til it breaks.

提交回复
热议问题