且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在objective-c++中将通知中心添加到cpp类

更新时间:2023-11-12 19:32:47

不,你不能那样做.

Objective-C 和 C++ 类层次结构是分开的.在 C++ 上下文中,self 未定义,但 this 已定义.在 Objective-C 上下文中,this 没有定义,而 self 是.

The Objective-C and C++ class hierarchies are separate. In a C++ context, self is not defined but this is defined. In an Objective-C context, this is not defined and self is.

此外,addObserver 接受一个 id 参数.C++ 对象实例不被视为 id 类型,因此即使您更改代码以传递 this 而不是 self,它也不会编译.

Also, addObserver takes an id parameter. C++ object instances are not considered of type id so even if you change your code to pass this instead of self it won't compile.

参见 C++ 限制来自 Objective-C 编程语言手册.

See the C++ Limitations from the Objective-C Programming Language Manual.

作为一种解决方法,创建一个 Objectve-C 类,该类具有您希望通知转到的 C++ 对象的实例,并在 Objective-C 对象收到通知时简单地调用该 C++ 方法.

As a workaround, create an Objectve-C class which has an instance of the C++ object you want the notification to go to, and simply call the C++ method on it when the Objective-C object gets the notification.