开发者

How to check what type a variable is?

I want to know if someVariable is of type int or float. How would I do the check?

Edit: In code at runtime! In the IDE that would be simple. I want to know if there is some kind开发者_开发技巧 of typeof operator to see if you're dealing with int, float, long, char, unsigned char, long long or whatever.


if ((1?1:var)/2) {
    /* it's floating point */
} else {
    /* it's an integer */
}


Is someVariable an NSNumber? If it's a primitive C type (an actual int or float), then you'll already know because it can be declared as only one of those things, and the compiler (and Xcode) will enforce that. As another commenter noted, Command-Double-Click the name to find the declaration in Xcode.


Since sizeof(float) == sizeof(int) (depending on platform, but on i386 and current iPhone this is valid) - i believe you can't decide during runtime whether your variable is int or float.
EDIT: Greg is right in the comment. This check doesn't really make sense.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜