Objective C 返回类型开关
我想使用选择器的返回类型来确定它在我的代码中的使用方式,有没有办法区分 Objective c 中的返回类型,我会给你一个例子。
SEL selectors[]=
{@selector(method1),
... //each method returns a different type
@selector(methodn);}
for (SEL sel in selectors)
{
switch [[self performSelector:sel]/*idk something here maybe?*/]
{
case int:
//do some stuff
...
case NSString *:
//do some other stuff
}
}
提前致谢,我在任何地方都找不到任何谈论 Objective C 的内容
I want to use the return type of a selector to determine how it is used in my code is there a way to differentiate the return types in objective c I'll give you an example.
SEL selectors[]=
{@selector(method1),
... //each method returns a different type
@selector(methodn);}
for (SEL sel in selectors)
{
switch [[self performSelector:sel]/*idk something here maybe?*/]
{
case int:
//do some stuff
...
case NSString *:
//do some other stuff
}
}
Thanks in advance I couldn't find anything anywhere on this that talked about objective c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过 objc 运行时中的
method_copyReturnType
来完成此操作。但是,返回和参数的 objc 类型都是相同的(我上次检查过),因此运行时不会返回带有描述“NSArray”的类型 - 它只是一个标识符objc 类型。尽管如此,这种详细程度对于您的
int
或NSString
情况来说已经足够描述了,您可以使用 NSObject 实例的class
或isKindOfClass :
(等)实例方法来确定它的类型,一旦你掌握了它的句柄。you can do this via
method_copyReturnType
in objc runtime.however, objc types for return and parameters are all the same (last i checked), such that the runtime will not return the type with the description "NSArray" -- it will just be the identifier for an objc type. nevertheless, that level of detail is descriptive enough for your
int
orNSString
case, and you can use an NSObject instance'sclass
orisKindOfClass:
(etc.) instance methods to determine its type once you have a handle on it.您可以使用
-methodSignatureForSelector:
获取方法的NSMethodSignature
。然后,您可以使用NSMethodSignature
对象中的-methodReturnType
获取返回类型。如果不需要,请不要扰乱运行时。You can get the
NSMethodSignature
of the method using-methodSignatureForSelector:
. And then you can get the return type with the-methodReturnType
from theNSMethodSignature
object. Don't mess with the runtime if you don't have to.