简体   繁体   中英

iOS: Unrecognized selector sent to instance

I have a class where I request information from a provider class, in which after finalizing the job (asynchronous httpRequest block) needs to invoke a method [- (void) updateCountries] in the requester class. If I am not wrong this code worked in iOS 7, but now in iOS 8 it does not.

Can you please help me to understand why?

Methods in requester class:

- (void) viewWillAppear:(BOOL)animated {
    //get countries to pickerView
    webAPI = [[WebAPI alloc] init];
    [webAPI retrieveCountries:self];
}

- (void) updateCountries {
    //update countries content for pickerView
    locationDAO = [[LocationDAO alloc] init];
    countriesArray = [locationDAO getCountries];

    [pickerView reloadAllComponents];
}

Lines in method in provider class where error happens:

SEL updateCountries = sel_registerName("updateCountries:");
[requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];

If you need to checkout the entire method in the provider class, here it is:

- (void) retrieveCountries:(id)requester {

//    NSLog(@"engine report: firing retrieveCountries http get");

    NSString *urlAsString = kRetrieveCountriesListAPI;
    NSURL *url = [NSURL URLWithString:urlAsString];



    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    [urlRequest setTimeoutInterval:30.0f];
    [urlRequest setHTTPMethod:@"GET"];
    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-type"];


    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        if ([data length] >0 && error == nil){
            NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"engine report: retrieveCountries server response: %@", response);

            NSArray *level0 = [[NSArray alloc] initWithObjects:[NSJSONSerialization JSONObjectWithData:[[NSData alloc] initWithData:data] options:kNilOptions error:&error], nil];

            NSArray *level1 = [level0 objectAtIndex:0];

            LocationDAO *locationDAO = [[LocationDAO alloc] init];
            [locationDAO deleteAllFromCountries];

            for (int i = 0; i < [level1 count]; i++) {

                CountryVO *countryVO = [[CountryVO alloc] init];

                countryVO.myID = [[[level1 objectAtIndex:i] objectForKey:@"id"] integerValue];
                countryVO.name = [[level1 objectAtIndex:i] objectForKey:@"country_name"];


                [locationDAO saveCountryToDatabase:countryVO];

            }

            SEL updateCountries = sel_registerName("updateCountries:");

            [requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];

            dispatch_async(dispatch_get_main_queue(), ^(void){

            });


        } else if ([data length] == 0 && error == nil){
            NSLog(@"Nothing was downloaded.");

        } else if (error != nil) {
            NSLog(@"Error happened = %@", error);
        } }];

}

THANK YOU A WHOLE LOT

Remove the : from the selector specification:

SEL updateCountries = sel_registerName("updateCountries");

Your method updateCountries doesn't take any arguments. So, when creating the selector , you should only write updateCountries (instead of updateCountries: which would indicate that this method takes an argument).

The reason why your app crashes is that when you try to perform this selector, the internals of your app are looking for a method called updateCountries on requester that takes one argument. This method doesn't exist, which is why the app crashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM