如何检查变量是否为对象? [英] How to check if a variable is an object?

查看:64
本文介绍了如何检查变量是否为对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在编译时执行以下操作?

Is there any way to do the following at compile-time?

int anInteger = 0;
__if_object(anInteger) {
    // send object some messages
}
__if_primitive(anInteger) {
    // do something else
}

可以使用的一种虚拟情况是定义下面的 __add_macro.

An dummy situation where this could be used is to define the __add_macro below.

#define __add_macro(var, val) __something_goes_here__

int i = 1;
MyInteger* num = [[MyNumber alloc] initWithValue:1]

__add_macro(i, 4);
__add_macro(num, 4);

// both should now hold 5

澄清/简化

我想没有办法用一个宏来做到这一点.但是我仍然需要它来警告是否在错误的数据类型上使用了宏.这两种类型是:objectnon-object).

I guess there is no way to do this with one macro. But I still need it to warn if the macro is being used on the wrong datatype. Those two types are: object and non-object).

要检查它是否是一个对象,这是有效的:

To check if it is an object, this works:

#define __warn_if_not_object(var) if(0){[(var) class];}

我需要什么:

#define _warn_if_object(var) if(0){__something_here__}

同样,我需要在编译时发生这种情况.它可以抛出错误或警告.

Again, I need this to happen at compile-time. And it can either throw an error or warning.

谢谢

推荐答案

当你声明一个 int 变量时,你实际上只能在其中放入一个 int 值.

When you declare an int variable you can really only put an int value in it.

虽然这是Objective-C,因此也是C,因此您可以绕过几乎所有存在的类型保护机制,但建议.实际上,无法保证 NSNumber 引用甚至适合 int 变量 - 如果您尝试并绕过任何警告,则有足够的机会,一些位将被丢弃,使引用无效.

While this is Objective-C, and hence C, so you can bypass just about every type protection mechanism that exists, this is not to be advised. Indeed there is no guarantee whatsoever that a, say, NSNumber reference will even fit into an int variable - and more than enough chance that if you try, and bypass any warnings, some bits will just get tossed making the reference invalid.

所以,不,虽然您可以判断一个对象引用指的是哪个类,但通常您无法判断一个变量是否具有整数值或对象引用 - 您甚至不应该尝试将这两个完全不同事情变成同一个变量.

So, no, while you can tell what class an object reference refers to, you cannot in general tell whether a variable has an integer value or an object reference in it - you shouldn't even try to put these two very different things into the same variable.

答案 2

Patrick,您的评论和澄清似乎表明您并没有试图通过提问来做问题的开始(您如何确定 int 中的值是否为对象 - 上面已回答,你没有),但有些不同......

Patrick, your comments and clarification seem to suggest you are not trying to do what the question starts out by asking (how do you determine if the value in an int is an object - answered above, you don't), but something rather different...

我认为您所追求的是函数重载,而且您似乎在尝试使用宏,也可能是内联函数.Clang 支持函数重载,这里是程序片段,可以告诉你如何解决你的问题:

I think what you're after is function overloading, and as you seem to be trying to use macros, maybe inline functions as well. Clang supports function overloading, here is program fragment which may show you how to solve your problem:

// Clang likes prototypes so let's give it some
// The following declares two overloaded inline functions:
NS_INLINE void __attribute__((overloadable)) byType(int x);
NS_INLINE void __attribute__((overloadable)) byType(NSNumber *x);

// now some simple definitions:
NS_INLINE void __attribute__((overloadable)) byType(int x)
{
   NSLog(@"int version called: %d", x);
}

NS_INLINE void __attribute__((overloadable)) byType(NSNumber *x)
{
   NSLog(@"NSNumber version called: %@", x);
}

// now call them, automatically selecting the right function
// based on the argument type
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
   int x = 5;
   NSNumber *y = [NSNumber numberWithInt:42];

   byType(x);
   byType(y);
}

以上代码运行时输出:

int version called: 5
NSNumber version called: 42

Clang 3 编译上述代码内联两个调用,因此您获得与使用宏相同的代码.

Clang 3 compiles the above code inlining the two calls, so you get the same code as using macros.

这篇关于如何检查变量是否为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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