NS调用;变量不是 CFString

发布于 2024-10-18 12:18:40 字数 802 浏览 3 评论 0原文

我正在使用 NSInspiration 进行动态调用:

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”。更糟糕的是;所有三个变量都指向同一内存位置。

怎么会这样呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-10-25 12:18:40

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

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

那么你应该写:(

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

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文