1

Because of some essential C++ libraries, I have to mix Objective-C and C++ in my code. But the problem is that the callback between C and Objective-C is extremely hard to design and manipulate, so I found NSNotification to the rescue.

Many functions in my C++ library are asynchronously running and I found NSNotification can help me a lot. By sending unique group of notifications, my C++ libraries and Objective-C manager controlling them can communicate smoothly.

But is it a good design? The other ways are limited including Objective-C running time binding or using block.

2 Answers 2

1

NSNotification is a great tool to use if it fits your needs. If you want a straight C interface, you can always use CFNotification from CoreFoundation instead.

In terms of its design, it lets you decouple unrelated classes that need to communicate. Instead of having to have your C++ class know that the object that wants to know when something changes is an Objective-C class of a particular type, you can simply have it post a notification when an event happens. Any and all other objects in the program can choose to listen for that notification or ignore it.

Of course, when classes are loosely coupled, it can be harder to debug problems since you don't have a direct pointer to the object that set up the notification, and probably don't have a stack frame that points to where the issue started. But usually the ease with which you can add and remove listeners is enough of a benefit to make it worth doing anyway. You can always put something in the userInfo dictionary of the notification that points back to the object that sent the notification if you need something like that for debugging.

2
  • Actually, you have a stack frame. Notifications are effectively just slightly complicated function calls. The code sending the notification will be on the call stack, with some OS code in between.
    – gnasher729
    Commented Aug 4, 2018 at 16:42
  • Really? That's not been my experience, though I tend to work on macOS more than iOS. What I'm used to seeing is the notifying code posts the notification then returns to its caller. Later, the event loop handles processing notifications after handling other things like timers and UI events. So by the time your notification callback gets called, the poster of the notification is long gone from the call chain. But perhaps things have changed recently, or I'm thinking of a different notification system? Commented Aug 4, 2018 at 17:40
1

From an implementation point of view, sending an NSNotification is exactly the same as calling all the methods and blocks that have registered to observe the notification in an unspecified order.

So the use case for NSNotification is that your code is doing something and assumes that other code should know about it, but doesn't have the knowledge or doesn't want the knowledge to call appropriate methods, probably in wildly varying classes. It's a very useful feature, even in purely Objective-C code.

The easiest way to combine C++ and Objective-C is writing Objective-C++ code.

2
  • But the C++ library needs some inheritance so that's why I cannot implement it in Objective-C class :( Cannot find a better way
    – Patroclus
    Commented Aug 5, 2018 at 6:38
  • Well, you can always create an Objective-C++ class that contains a pointer to a C++ object or its subclass.
    – gnasher729
    Commented Aug 5, 2018 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.