Crash when using performSelector: with a SEL passed from another class

≡放荡痞女 提交于 2019-12-04 02:31:30

问题


Is it possible to send a selector to another class and have it perform on that other class?

This seems to crash with the error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[webManager _selector]: unrecognized selector sent to instance. If this is not possible, what would you recommend as an alternative?

The methods are placed in the order that they are performed.

//in HomeViewController
-(void)viewDidLoad
{
    WebManager *webManager = [[WebManager alloc] init];
    URLCreator *urlCreator = [[URLCreator alloc] initWithParam:@"default"];
    [webManager load:[urlCreator createURL] withSelector:@selector(testSelector)];
}
//in WebManager, which is also the delegate for the URL it loads when the load method is called...
-(void)load:(NSString*)url withSelector:(SEL)selector
{
    _selector = selector;    
    [self load:url];
}

-(void)load:(NSString*)url{
    NSURLRequest * request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

//now the delegate response, ALSO in WebManager
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"Connection did finish loading. Parsing the JSON");

    [self getJSONDict:rawData]; //parses the data to a dictionary

    //perform the selector that is requested if there is one included
    if (_selector)
    {
        NSLog(@"Performing selector: %@", NSStringFromSelector(_selector));
        //the selector is called testSelector
        [self performSelector:@selector(_selector)];    
    }
}

- (void)testSelector //also in the WebManager class
{
    NSLog(@"Selector worked!");
}

回答1:


This is your problem:

[self performSelector:@selector(_selector)];

A SEL is a type representing the name of a method. @selector is a compiler directive which converts the literal text inside the brackets into a SEL.

But _selector, your ivar, already contains a SEL. You're converting the text "_selector" into a SEL, and then trying to use that. Since no method with the selector "_selector" exists in the target class, you get an exception.

Change the line to:

[self performSelector:_selector];

and everything should be dandy. This uses the SEL which you already have stored in the variable _selector.

Also, generally speaking, please post your real code initially.




回答2:


[self performSelector:@selector(_selector)];   

The code above actually translates to [self _selector] which I'm assuming is not what you want. You should change your code to

[self performSelector:_selector];   


来源:https://stackoverflow.com/questions/9299536/crash-when-using-performselector-with-a-sel-passed-from-another-class

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