NSIncation setSelector 抛出异常

发布于 2024-11-15 21:18:50 字数 637 浏览 0 评论 0原文

我正在测试以下代码:

NSString * parameterSignature = @"@:";
NSMethodSignature * signature = [NSMethodSignature signatureWithObjCTypes:[parameterSignature UTF8String]]; 
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(aMethodWithNoParms)];

当 setSelector 执行时,我收到此错误:

Name: NSInvalidArgumentException
File: Unknown
Line: Unknown
Reason: -[NSInvocation setArgument:atIndex:]: index (1) out of bounds [-1, 0]

我一直在网上寻找原因,但没有找到它。看起来它正在尝试设置调用的第二个参数,即选择器,但数组不够长。我本以为调用的创建会设置数组。

我不知道如何解决这个问题,有人看到我做错了什么吗?

I have the following code which I'm testing:

NSString * parameterSignature = @"@:";
NSMethodSignature * signature = [NSMethodSignature signatureWithObjCTypes:[parameterSignature UTF8String]]; 
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(aMethodWithNoParms)];

When the setSelector executes I get this error:

Name: NSInvalidArgumentException
File: Unknown
Line: Unknown
Reason: -[NSInvocation setArgument:atIndex:]: index (1) out of bounds [-1, 0]

I've been trolling the net looking for the reason why and not found it. It looks like it's trying to set the second parameter of the invocation which would be the selector, but the array is not long enough. I would have thought that the creation of the invocation would have setup the array.

I'm not sure how to fix this, anyone see what I've done wrong?

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

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

发布评论

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

评论(2

我不咬妳我踢妳 2024-11-22 21:18:50

如果您的方法不返回任何内容,即它具有 void 返回类型,那么您的方法签名将为,

NSString * parameterSignature = @"v@:";

指示 void 返回类型,self_cmd

您最好使用 methodSignatureForSelector:instanceMethodSignatureForSelector: 来获取方法签名。

If your method doesn't return anything i.e. it has a void return type, then your method signature will be,

NSString * parameterSignature = @"v@:";

indicating void return type, self and _cmd.

You should preferably use methodSignatureForSelector: or instanceMethodSignatureForSelector: to get the method signature.

满意归宿 2024-11-22 21:18:50

我认为 C 字符串的第一个字符应该是返回类型。 则应该如此

"@@:"

如果该方法返回一个对象, 。您的字符串定义了一种方法,该方法具有对象的返回类型和一个选择器参数。事实上,至少需要返回类型、接收器类型和选择器类型。

请参阅此讨论

I thiink the first character of the C string should be the return type. So it should be

"@@:"

if the method returns an object. Your string defines a method with a return type of object and one parameter which is a selector. In fact, at the minimum you need the return type, the receiver type and the selector type.

See this discussion.

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