Jake Petroules
Jake Petroules

Reputation: 24188

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:

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.

Upvotes: 4

Views: 434

Answers (2)

PeyloW
PeyloW

Reputation: 36762

You can not consume the notification with a C++ method directly, or simple C function for that matter.

You must wrap the call to the C++ method is an actual Objective-C method, or block, and then delegate forward the notification to the C++ method.

Upvotes: 2

bbum
bbum

Reputation: 162722

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

Upvotes: 3

Related Questions