NSIncation setSelector 抛出异常
我正在测试以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的方法不返回任何内容,即它具有
void
返回类型,那么您的方法签名将为,指示
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,indicating
void
return type,self
and_cmd
.You should preferably use
methodSignatureForSelector:
orinstanceMethodSignatureForSelector:
to get the method signature.我认为 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.