SO tells me that this question is subjective and will probably be closed. This is really subjective, because I ask the opinions of experienced Objective-C developers. Should I post this elsewhere? Please inform.
Quite unexpectedly for Objective-C, although it is pretty confident in the concept of writing OOP code, from the very beginning I struggled with the NSNotification vs Delegate dilemma. I wrote a few questions on this. I think I understand the point. Notifications are sent worldwide, so they cannot be used to notify nearby objects. Delegates exist to transfer tasks to another entity acting on behalf of the delegated entity. Although this can be used for closely related objects, I believe that the workflow is detailed (new class, new protocol, etc.), and only the word "delegation" makes me think about armies and bosses and generally makes me feel embarrassing.
Where I come from (AS3), there are things called events. They are halfway between delegates and NSNotifications, and largely manage the world of flash notifications, until recently, Mr. Robert Penner came and expressed his dissatisfaction with the events. So he wrote a library that is now widely used in the AS3 community called Signals. Inspired by C # events and signals / slots in Qt, these signals are actually properties of objects that you receive from the outside and add listeners. There you can do much more with the signal, but on it the core is what it is.
Since the concept is so modest, I gave it and wrote my own class of signals in Objective-C. I used Signal.h / .m here .
A way to use this to notify class A of an event in class B might look like this:
I hope that he adheres to the correct memory management rules, but when the signal is canceled, he should automatically remove all listeners and skip silently. Such a signal should not be a general substitute for notification and delegation, but there are many close situations where I feel that the signal is cleaner than the other two.
My question for stackoverflow is what do you think of such a solution? Can you remove this from your project immediately if one of your interns puts it in? Would you fire your employee if he has already completed an internship? Or maybe there is already something similar, but much more grandiose than you use instead?
Thanks for your time, EP.
EDIT: Let me give you a concrete example of how I used this in an iOS project.
Consider this scenario of four types of property with nested ownership. There is a view manager that owns a window manager that owns several windows, each of which has a view with controls, including a close button. There is probably a design flaw here, but this is not an example point: P
Now that the close button is pressed, the gesture recognizer starts the first selector in the window object. It is necessary to notify the window manager that it is closing. Then the window manager can decide whether another window will appear, or if all windows remain hidden all together, and at this moment the view manager needs to get a relief to enable scrolling on the main view.
The notifications from the window to the window manager and the window manager for viewing the controller are those that I have now implemented using signals. Perhaps this was a case of delegation, but for just a “closed” action, it seems pretty verbose to create two delegate protocols. On the other hand, since the relationship of these objects is very well defined, this is also not like the case of NSNotifications. There also has not changed the value that I could observe with KVO, because it is just a click of a button. Listening to some kind of "hidden" state might make me have this flag reset when the window is reopened, which makes it difficult to understand and a little error prone.