How to check if a variable is an object?
objective-c
Solution
When you declare an `int` variable you can really only put an `int` value in it.
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.
Answer 2
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...
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);
}
The above code when run outputs:
int version called: 5
NSNumber version called: 42
Clang 3 compiles the above code inlining the two calls, so you get the same code as using macros.
Problem
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 } ``` 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 ``` Clarification/Simplification 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];} ``` What I need: ``` #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. Thanks