Search code examples
iphoneobjective-cipadnsinvocation

NSInvocation; variable is not a CFString


I'm making a dynamic call using NSInvocation:

NSInvocation *lNSInvocation = [NSInvocation invocationWithMethodSignature: [lListener methodSignatureForSelector:lSelector]];
[lNSInvocation setTarget:lListener];
[lNSInvocation setSelector:lSelector];
// Note: Indexes 0 and 1 correspond to the implicit arguments self and _cmd, which are set using setTarget and setSelector.
[lNSInvocation setArgument:object atIndex:2];
[lNSInvocation setArgument:object2 atIndex:3];
[lNSInvocation setArgument:object3 atIndex:4];
[lNSInvocation invoke];

In the debugger all three object variables correctly point to three different NSCFString*. The invocation is done and on the other side the correct method is reached.

- (void)login:(NSString*)username password:(NSString*)password host:(NSString*)host

However, in the debugger its parameters give an error: "variable is not a CFString". Even worse; all three variables point to the same memory location.

How can this be?


Solution

  • If the method arguments are objects, -setArgument:atIndex: expects a pointer to the variable from which the object can be copied. Consequently, if your strings are:

    NSString *object = @"…";
    NSString *object2 = @"…";
    NSString *object3 = @"…";
    

    then you should write:

    [lNSInvocation setArgument:&object atIndex:2];
    [lNSInvocation setArgument:&object2 atIndex:3];
    [lNSInvocation setArgument:&object3 atIndex:4];
    

    (note the ampersand before each object argument)