Why does this NSInvocation raise an exception?

Deadly 提交于 2019-12-12 15:46:30

问题


I have a real head-scratcher right now. So, an NSTimer object, an NSMethodSignature object, and an NSInvocation object walk into a bar. Here's the rest of the joke:

NSMethodSignature *methodSig = [NSMethodSignature methodSignatureForSelector:@selector(setAlphaValue:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSig];
CGFloat alphaVal = 1.f;

[inv setSelector:@selector(setAlphaValue:)];
[inv setTarget:tabViewItem.view];
[inv setArgument:&alphaVal atIndex:2];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5f invocation:inv repeats:NO];

Here's what I'm getting in the debugging console:

+[NSInvocation _invocationWithMethodSignature:frame:]: method signature argument cannot be nil

Edit: I'm not sure why someone thought it was necessary to down-vote my question. Sorry for trying to learn something new. Actually, here's an amendment to my original question: what should I have done differently? Should I have just deleted the post once I figured out what my problem was? I tried to follow all of the Stack Overflow etiquette, and I even took the time to leave an answer in the off chance it could help someone else. Next time, should I just return to my question and leave a response like, "nvm … figured it out. thx?" Or should I just leave it here, unanswered? I sure as hell know I'm tired of clicking on links only to be led to unanswered posts.


回答1:


Oops. I jumped the gun. Xcode's code completion had me thinking that methodSignatureForSelector: was a class method of NSMethodSignature. Whenever I'm dealing with classes I don't usually mess with on a regular basis, I generally start by typing [NSClassImUnfamiliarWith, followed by a space. Doing so brings up Xcode's code completion popup, which generally has all of the methods one can call. I accidentally chose an inherited (?) method from NSObject thinking it was a class method. I guess this would be an example of polymorphism. Anyway, I solved my issue by replacing NSMethodSignature with my object: tabViewItem.view. So all together it looks like this:

NSMethodSignature *methodSig = [tabViewItem.view methodSignatureForSelector:@selector(setAlphaValue:)];

I don't know if this will help anyone else, but by golly, I'm gonna post this just in case. Good luck!



来源:https://stackoverflow.com/questions/20039600/why-does-this-nsinvocation-raise-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!