Search code examples
c++objective-ccocoaobjective-c++nsnotifications

How to receive NSNotifications from Objective-C in C++ classes?


I have an Objective-C++ class that adds itself as an observer for an event on a Cocoa NSView. I would like to be able to send the NSNotifications to a method of a C++ class instead of an Objective-C method or block. How can I do this?

My situation is this:

  • A - Objective-C++ class
  • B - NSView

B is encapsulated by A. I want to be notified of one of B's events. However, the method handling that event MUST have a reference to the instance of A that contains B.


Solution

  • Create a dead simple wrapper class in Objective-C that points to your C++ instance and handles the notification by calling the C++ method.