传递本地指针到performSelectorOnMainThread的withObject参数? [英] Passing a native pointer to performSelectorOnMainThread's withObject argument?

查看:173
本文介绍了传递本地指针到performSelectorOnMainThread的withObject参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个C ++对象到使用performSelectorOnMainThread调用的选择器。简单地将指针转换为一个'id'(objc_object *)似乎不工作。
我总是可以写我自己的目标C包装类,但应该有一个相当标准的方式做到这一点。

I want to pass a point to a C++ object to the selector invoked using performSelectorOnMainThread. Simply casting the pointer to an 'id' (objc_object*) doesn't seem to work. I could always write my own objective C wrapper class, but there should be a fairly standard way of doing this. I didn't really find anything on apple's documentation for it though.

这是最好的方法是什么?

What's the best way to do this?

这里是我想做的:

...
Foo *foo = new Foo(); // Foo is a C++ class
MyObject *myObj = [[MyObject alloc] init]; // Obj-C class
[myObj performSelectorOnMainThread:@selector(someMethod:) withObject:foo waitUntilDone:NO];
...

谢谢!

推荐答案

不需要创建自己的包装类; [NSValue valueWithPointer:]是为拳击指针设计的。只要让someMethod取一个NSValue而不是一个原始指针,然后用pointerValue提取指针。

No need to make your own wrapper class; [NSValue valueWithPointer:] is designed for boxing pointers. Just make someMethod take an NSValue rather than a raw pointer, and then extract the pointer with pointerValue.

(我相信performSelector *方法保留他们的参数,保证通过调用持续,这就是为什么你不能只是将随机值转换为该参数中的id。)

(I believe that the performSelector* methods retain their arguments so that the object is guaranteed to last through the call, which is why you can't just cast a random value to an id in that argument.)

这篇关于传递本地指针到performSelectorOnMainThread的withObject参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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