Determine if (void *) a pointer from NSInvocation getArgument is object or primitive

Struggle with that. Hoping this is possible and I don't look stupid.

I am hacking forwardInvocation in the class I am writing. What I want to do is redirect the call to one selector or another, depending on whether it is an object or a primitive type. My ultimate goal is to “insert” primitives so that they can be added to arrays / dictionaries. For simplicity, the two types of values ​​that usually go here are NSStrings and enumerations.

In short, given a pointer, is there a way to determine if this is an object?

__unsafe_unretained id argument;
[anInvocation getArgument:&argument atIndex:2];

// EXC_BAD_ACCESS if primitive (i.e. NSInteger value of 2 ($1 = 0x00000002) )
if (![argument isKindOfClass:[NSObject class]]) {
    // Box the value
    ...
}

Is there a test that I can run? Now my hacker code does this nasty trick:

// All my enums have at most 10 elements. I'm so bad at code.
if ((NSInteger)argument < 10) {
    // Box the value
    ...
}

Thanks in advance.

+4
2

:

NSMethodSignature *signature = [invocation methodSignature];
const char* argType = [signature getArgumentTypeAtIndex:2];

Objective-C

, getArgument:atIndex::

, , , . .

__unsafe_unretained id argument;
[anInvocation getArgument:&argument atIndex:2];

argument , , sizeof(id)

+3

C - , . , void, . , , - , , - . , void, , , , , , - . Objective-C , , .

+3

Source: https://habr.com/ru/post/1527645/


All Articles