Need/want to pass an NSError** as an argument to performSelector

折月煮酒 提交于 2019-12-05 23:53:59

Check out NSInvocation, which lets you "performSelector" in a much more flexible way.

Here's some 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];
}

NSError** is not an object (id), which performSelector wants for each of the withObject args. You could go to NSInvocation, but that seems like a lot of work if this is just a single message you want to use. Try defining an intermediate selector method that takes as an arg your wrapped NSError** in an object, and let that method do the performSelector on it (which I think was probably what you meant at the end of your question?)

I'm not positive, but you may want to take a look at using an NSInvocation instead of -performSelector:withObject:withObject. Since NSInvocation takes arguments of type void*, it may/should let you set whatever you want as an argument.

It'll require several more lines of code than a simple performSelector: call, but it'll probably be more convenient than wrapping your pointer in an object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!