Need / need to pass NSError ** as an argument to make a selection

I want to call a method selector that has the usual argument NSError **:

-(int) getItemsSince:(NSDate *)when dataSelector:(SEL)getDataSelector error:(NSError**)outError  {
    NSArray *data = nil;
    if([service respondsToSelector:getDataSelector]) {
        data = [service performSelector:getDataSelector withObject:when withObject:outError];
        // etc.

... which the compiler does not like:

warning: passing argument 3 of 'performSelector:withObject:withObject:' from incompatible pointer type

Is there any way around this to not encapsulate the pointer in the object?

+3
source share
3 answers

Check NSInvocation , which allows you to “make selections” in a more flexible way.

Here is the code to get you started:

if ([service respondsToSelector:getDataSelector]) {
    NSArray *data;
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
        [service methodSignatureForSelector:getDataSelector]];
    [invocation setTarget:delegate];
    [invocation setSelector:getDataSelector];
    // Note: Indexes 0 and 1 correspond to the implicit arguments self and _cmd, 
    // which are set using setTarget and setSelector.
    [invocation setArgument:when atIndex:2]; 
    [invocation setArgument:outError atIndex:3];
    [invocation invoke];
    [invocation getReturnValue:&data];
}
+14
source

NSError ** - (id), Selector withObject. NSInvocation, , , . , NSError ** , performSelector (, , , ?)

+3

, NSInvocation -performSelector:withObject:withObject. NSInvocation void*, / , , .

, performSelector:, , , , .

+2
source

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


All Articles