开发者

NSInvocation making app crash

开发者 https://www.devze.com 2023-01-03 04:08 出处:网络
I\'m using NSInvocation as follows: In my init, I\'m writing this in my viewDidLoad: SEL mySelector; mySelector = @selector(initParsersetId:type:);

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 = [[开发者_JAVA百科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?


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.


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...


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消