【发布时间】:2014-07-16 11:15:51
【问题描述】:
-performSelector 方法的返回类型是id,我在 Apple 文档中找到了这个:
For methods that return anything other than an object, use NSInvocation.
但下面的代码运行良好:
BOOL boolValue = (BOOL)[self performSelector:@selector(boolValue)];
它可以返回id、BOOL、NSInteger等。我想知道怎么做?因为将BOOL或NSInteger转换为id在return语句中导致错误:
Cast of 'NSInteger' (aka 'long') to 'id' is disallowed with ARC
提前致谢!
----编辑----
感谢您的回答。
我知道这样做不好,我也知道如何使用NSInvocation,我只想知道-performSelector方法是如何实现的。
----编辑----
终于找到了……
https://opensource.apple.com/source/objc4/objc4-706/runtime/NSObject.mm
【问题讨论】:
-
将 object 强制转换为 primitive 在逻辑上并不正确。
-
@holex 是的,我知道。我只是想知道该怎么做。
-
您只能发回一个 object,例如
NSNumber,您可以在其中存储一个 primitive 值,例如BOOL、NSInteger、Float64等等......当你拥有对象时,你可以将它转换回 primitives。既不太优雅也不太安全——但很快。
标签: objective-c