运行 performSelector:在返回双精度值而不是 id 的对象上 [英] Running performSelector: on an object that returns a double, not id

查看:74
本文介绍了运行 performSelector:在返回双精度值而不是 id 的对象上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在对象上运行任意选择器,其返回值为双精度值?例如,我有 obj A,它有方法 -(double)b​​lah;

How can I run an arbitrary selector on an object, the return of which is a double? For example, I have obj A, which has method -(double)blah;

我该怎么做double res = [obj performSelector:@selector(blah)];?performSelector 返回一个 id 类型的对象,所以我应该从 id 转换为 NSInteger 到 double - 这会失去精度吗?

How can I do double res = [obj performSelector:@selector(blah)];? performSelector returns an id type object, so should I cast from id to NSInteger to double - that will lose precision?

另外,我不想想使用 obj 的 methodSignatureForSelector(意思是,没有 NSMethodSignature 和没有 NSInvocation) 因为它在运行时会消耗大量 CPU.

Also, I do not want to use the obj's methodSignatureForSelector (meaning, no NSMethodSignature and no NSInvocation) because it is a huge CPU drain at run-time.

推荐答案

使用最少的内存(valueForKey:使用两个临时对象)并且 msgSend 方法不起作用.

Uses least memory (valueForKey: uses two temporary objects) and the msgSend method doesn't work.

 IMP myImp1 = [obj methodForSelector:@selector(getDouble)];
 double aDouble1 = ((double (*) (id,SEL))myImp1)(obj,@selector(getDouble));

这篇关于运行 performSelector:在返回双精度值而不是 id 的对象上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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