我正在使用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不是一个变量”。更糟糕的是,所有三个变量都指向相同的内存位置。
这怎么可能呢?
发布于 2011-02-21 18:30:25
如果方法参数是对象,则-setArgument:atIndex:需要指向可从中复制对象的变量的指针。因此,如果您的字符串是:
NSString *object = @"…";
NSString *object2 = @"…";
NSString *object3 = @"…";然后你应该写下:
[lNSInvocation setArgument:&object atIndex:2];
[lNSInvocation setArgument:&object2 atIndex:3];
[lNSInvocation setArgument:&object3 atIndex:4];(请注意每个对象参数前的与号)
https://stackoverflow.com/questions/5064700
复制相似问题