NSIncallation 导致应用程序崩溃
我使用 NSInvocable 如下:
在我的 init 中,我在 viewDidLoad 中编写此内容:
SEL mySelector;
mySelector = @selector(initParsersetId:type:);
NSMethodSignature * sig = nil;
sig = [[self class] instanceMethodSignatureForSelector:mySelector];
myInvocation = nil;
myInvocation = [NSInvocation invocationWithMethodSignature:sig];
[myInvocation setTarget:self];
[myInvocation setSelector:mySelector];
我这样调用它:
Idea *tempIdea = [[Idea alloc]init];
tempIdea = [genericArray objectAtIndex:indexPath.row];
idea.ideaId = tempIdea.ideaId;
[tempIdea release];
NSNumber *_id_ = [NSNumber numberWithInt:idea.ideaId];
[myInvocation setArgument:_id_ atIndex:2]; //CRASHING AT THIS LINE
我的应用程序在指定的行崩溃。有人可以帮我吗?
I'm using NSInvocation as follows:
In my init, I'm writing this in my viewDidLoad:
SEL mySelector;
mySelector = @selector(initParsersetId:type:);
NSMethodSignature * sig = nil;
sig = [[self class] instanceMethodSignatureForSelector:mySelector];
myInvocation = nil;
myInvocation = [NSInvocation invocationWithMethodSignature:sig];
[myInvocation setTarget:self];
[myInvocation setSelector:mySelector];
And I'm calling it like this:
Idea *tempIdea = [[Idea alloc]init];
tempIdea = [genericArray objectAtIndex:indexPath.row];
idea.ideaId = tempIdea.ideaId;
[tempIdea release];
NSNumber *_id_ = [NSNumber numberWithInt:idea.ideaId];
[myInvocation setArgument:_id_ atIndex:2]; //CRASHING AT THIS LINE
My application is crashing at the indicated line. Can anybody please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从你的代码来看不是很清楚;然而,我看到了一些可疑的东西。希望它能为您提供一些有用的提示。
首先,我没有看到您保留实例(从 [NSInitation... 自动释放)。由于 [NSInitation...] 中的实例是自动释放的,因此您的类级别变量 myInspiration 在 viewDidLoad 事件之后不会保留它。
您代码中的第二件事是选择器是一种自定义构造函数,以 init 开头......我不确定您是否可以在同一实例中调用该事件。还有一点是,如果你要调用的init...方法返回self?应该是的。
您可以使用 NSLog 函数在选择器事件中输出一些消息。 NSLog 的所有消息都将出现在 XCode 的输出控制台中。
It is not very clear from your codes; however, I see something suspicious. Hopefully, it may provide your some helpful hints.
First, I don't see you retain the instance (auto released from [NSInvocation...). Since the instance from [NSInvocation...] is auto-released, your class level variable myInvocation would not retain it after viewDidLoad event.
Second thing in your codes is that the selector is a kind of customized constructor, beginning with init..... I am not sure if you can invoke the event within the same instance. Another point is that if your init... method to be invoked returns self? It should be.
You may output some messages in your selector event by using NSLog function. All the messages by NSLog will be in your XCode's output console.
我已经找到了答案,但我不知道如何找到答案。实际上,最初我是在 viewDidLoad 中编写所有初始化代码,并通过向其传递不同的参数来简单地重用 NSInspiration 对象,因为 NSInitation 是一个可变对象。它不起作用。然后我编写了一个方法,其中包含所有初始化代码,并在每次使用 NSInitation 对象时调用该方法,并且它有效......
I've found the answer but I'm not convinced how. Actually, initially I was writing all the initialisation code in viewDidLoad and simply reusing the NSInvocation object by passing it the different argument since NSInvocation is a mutable object. It didn't work. Then I wrote a method with all the initialisation code inside it and called that method every time I used the NSInvocation object and it worked...
您需要提供 setArgument: 您传递的参数的地址,而不是参数本身:
NOT
另外,您确定您的函数将 NSNumber 作为第一个参数吗?
You need to give setArgument: the address of the argument you are passing, and not the argument itself:
NOT
Also, are you sure your function takes an NSNumber as first argument?