[英]iOS: Unrecognized selector sent to instance
我有一個我從提供程序類中請求信息的類,在該類中,完成作業(異步httpRequest塊)后,需要在請求者類中調用方法[-(void)updateCountries]。 如果我沒有記錯的話,此代碼可在iOS 7中使用,但現在在iOS 8中則無效。
你能幫我理解為什么嗎?
請求者類中的方法:
- (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];
}
提供程序類中方法中發生錯誤的行:
SEL updateCountries = sel_registerName("updateCountries:");
[requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];
如果您需要在提供程序類中檢出整個方法,則為:
- (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);
} }];
}
謝謝你
卸下:
從選擇規范:
SEL updateCountries = sel_registerName("updateCountries");
您的方法updateCountries
不接受任何參數。 因此,在創建選擇器時 ,您只應編寫updateCountries
(而不是updateCountries:
這表明該方法接受一個參數)。
您的應用程序崩潰的原因是,當您嘗試執行此選擇器時,您的應用程序內部會在requester
程序上尋找一種名為updateCountries
的方法,該方法接受一個參數。 此方法不存在,這就是應用程序崩潰的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.