开发者

How to get the respondsToSelector method's return type when it returns primitive type value

I know that I can get the respondsToSelector method's return type with object_getClassName when it returns an object.But it doesn't work when it returns a primitive type like NSInteger. My app crashed when I init a NSString with a wrong SEL which returns a NSInteger.T_T W开发者_如何学编程hat only I can do now is make methods return a NSNumber value when I need them return primitive type. Must I do this? Is there some simple way to get the return type of a SEL when it returns primitive type ?


What's the problem with using NSNumber? If you know what the "type" of number is (i.e., integer, float), you can call -[NSNumber integerValue] or -[NSNumber floatValue] to get the primitive you need.

If you don't happen to know what primitive type will be coming back, you can use the objCType property of an NSNumber instance to get the C-style string containing the @encode() directives. These indicate how Objective-C data is encoded. See Type Encoding for more information.

Based on the type encoding C string you get back, you can respond according to whatever primitive type is actually stored inside your NSNumber instance and use the aforementioned methods to retrieve the right value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜