NSInvocation的;变量不是CFString

时间:2011-02-21 10:16:39

标签: iphone objective-c ipad nsinvocation

我正在使用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];

在调试器中,所有三个对象变量都正确指向三个不同的NSCFString *。调用完成,另一方面达到了正确的方法。

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

但是,在调试器中,它的参数会出错:“变量不是CFString”。更糟;所有三个变量都指向相同的内存位置。

这怎么可能?

1 个答案:

答案 0 :(得分:2)

如果方法参数是对象,-setArgument:atIndex:需要一个指向可从中复制对象的变量的指针。因此,如果你的字符串是:

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

然后你应该写:

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

(注意每个对象参数前的&符号)