如何从c函数获取数据到cocoa(objective c)函数

发布于 2024-12-28 14:59:49 字数 818 浏览 0 评论 0原文

我正在使用 CFMessagePortRef 进行进程间通信。为了接收数据,我有回调方法

CFDataRef didReceiveDataFromOtherProcess(CFMessagePortRef local,SInt32 msgid,CFDataRef data,void *info)

现在我想将接收到的数据发送到我的主控制器。我可以在 didReceiveDataFromOtherProcess 中创建主控制器对象并将数据发送到主控制器,但我想编写通用消息传递模块。我无法在回调函数中访问实例变量,委托在这种情况下不起作用,因此我定义了 -(void)saveObject:(id)object 方法并将主控制器的对象保存在全局变量中。

messagePassing *object = [[messagePassing alloc]init];
[object saveObject:self]; //in main controller

//in messagePassing
-(void)saveObject:(id)object
{
globalObject = object;
}

CFDataRef didReceiveDataFromOtherProcess(CFMessagePortRef local,SInt32 msgid,CFDataRef data,void *info)
{
//....
[globalObject didReceivedData:(id)data]; // sending to main controller
}

但在这种情况下;如果我打开两个连接,它将更改我的全局对象。
有人可以帮我吗?

I am using CFMessagePortRef for inter process communication. for receiving data , i have call back method

CFDataRef didReceiveDataFromOtherProcess(CFMessagePortRef local,SInt32 msgid,CFDataRef data,void *info)

Now i want to send received data to my main controller. i can create main controller object in didReceiveDataFromOtherProcess and send data to main controller, but i want to write generalize message passing module. i am not able to access instance variable in callback function, delegate will not work in this case, so i defined -(void)saveObject:(id)object method and saving object of main controller in global variable.

messagePassing *object = [[messagePassing alloc]init];
[object saveObject:self]; //in main controller

//in messagePassing
-(void)saveObject:(id)object
{
globalObject = object;
}

CFDataRef didReceiveDataFromOtherProcess(CFMessagePortRef local,SInt32 msgid,CFDataRef data,void *info)
{
//....
[globalObject didReceivedData:(id)data]; // sending to main controller
}

but in this case; if i will open two connection it will change my global object.
Can anyone please help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2025-01-04 14:59:50

这就是 info 的用途。设置回调时,将 self 作为 info 指针传递。然后在回调中取消引用它,以便您可以与原始对象进行通信。

小心内存管理。如果 self 在回调之前被释放,那么释放它时就会崩溃。确保删除 dealloc 中的回调注册,这样就不会发生这种情况。

This is what info is for. Pass self as the info pointer when you set up the callback. Then dereference it in the callback so that you can communicate with the originating object.

Be careful about memory management. If self is deallocated before the callback, you'll crash when you deallocate it. Make sure to remove your callback registration in dealloc so this can't happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文