是否可以在objective-c++中将通知中心添加到cpp类 [英] Is it possible to add Notification Center to a cpp class in objective-c++

查看:59
本文介绍了是否可以在objective-c++中将通知中心添加到cpp类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 cpp 类中添加通知中心,可以吗????

I would like to add notification center from a cpp class, is it possible????

如果是这样,我该怎么做?

If so, how do I do it?

Manager.h

class Manager
{

  Manager();

};

Manger.mm 文件

Manger.mm file

Manager:Manager()
{
[[NSNotificationCenter dafaultCenter] addObserver:self selector:@selector(workermethod) name:@" Name" object:(id)nil];
}

我的编译器给出一个错误,说 self 没有声明......是的,我知道这是一个明显的错误.

My compiler gives an error saying self is not declared.......yes i know it is an obvious error.

因为我不是从 NSObject 派生的....请告诉我是否可以将通知中心添加到 Cocoa 中的 cpp 类??

Since I'm not deriving from NSObject.... Please let me know if it is possible to add notification center to cpp class in Cocoa??

推荐答案

不,你不能那样做.

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.

这篇关于是否可以在objective-c++中将通知中心添加到cpp类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆