NSIncallation 导致应用程序崩溃

发布于 2024-09-05 12:45:01 字数 753 浏览 5 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(3

鸩远一方 2024-09-12 12:45:01

从你的代码来看不是很清楚;然而,我看到了一些可疑的东西。希望它能为您提供一些有用的提示。

首先,我没有看到您保留实例(从 [NSInitation... 自动释放)。由于 [NSInitation...] 中的实例是自动释放的,因此您的类级别变量 myInspiration 在 vi​​ewDidLoad 事件之后不会保留它。

您代码中的第二件事是选择器是一种自定义构造函数,以 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.

爱的十字路口 2024-09-12 12:45:01

我已经找到了答案,但我不知道如何找到答案。实际上,最初我是在 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...

末が日狂欢 2024-09-12 12:45:01

您需要提供 setArgument: 您传递的参数的地址,而不是参数本身:

[myInvocation setArgument:&_id_ atIndex:2];

NOT

[myInvocation setArgument:_id_ atIndex:2];

另外,您确定您的函数将 NSNumber 作为第一个参数吗?

You need to give setArgument: the address of the argument you are passing, and not the argument itself:

[myInvocation setArgument:&_id_ atIndex:2];

NOT

[myInvocation setArgument:_id_ atIndex:2];

Also, are you sure your function takes an NSNumber as first argument?

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