How to add an observer to NSNotificationCenter in a C++ class using Objective-C++?

前端 未结 3 1969
一整个雨季
一整个雨季 2021-01-05 08:08

I have a C++ class that I recently renamed from *.cpp to *.mm to support objective-c. So I can add the following objective-c code.

[[NSNotificationCenter defa         


        
3条回答
  •  日久生厌
    2021-01-05 09:00

    You can't add a C++ method as an observer because of how Objective-C method handles method invocation vs C++. You must have an Objective-C class (Declared with @interface Class .. @end) to respond to those methods.

    Your only option is to wrap your C++ class in an Objective-C class, or just have a very light wrapper that just has a reference to an object and calls a method statically once the notification arrives.

提交回复
热议问题