iOS: 无法识别的选择器发送到实例
iOS: Unrecognized selector sent to instance
我有一个 class,我从供应商 class 请求信息,其中在完成作业(异步 httpRequest 块)后需要调用一个方法 [- (void) updateCountries]请求者 class。如果我没记错的话,这段代码在 iOS 7 中有效,但现在在 iOS 8 中无效。
你能帮我理解为什么吗?
请求者中的方法 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];
}
提供程序 class 方法中发生错误的行:
SEL updateCountries = sel_registerName("updateCountries:");
[requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];
如果您需要检查提供程序中的整个方法 class,这里是:
- (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:
,这表明此方法需要一个参数)。
您的应用程序崩溃的原因是,当您尝试执行此选择器时,您的应用程序的内部正在寻找一种名为 updateCountries
on requester
的方法,该方法接受一个参数。此方法不存在,这就是应用程序崩溃的原因。
我有一个 class,我从供应商 class 请求信息,其中在完成作业(异步 httpRequest 块)后需要调用一个方法 [- (void) updateCountries]请求者 class。如果我没记错的话,这段代码在 iOS 7 中有效,但现在在 iOS 8 中无效。
你能帮我理解为什么吗?
请求者中的方法 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];
}
提供程序 class 方法中发生错误的行:
SEL updateCountries = sel_registerName("updateCountries:");
[requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];
如果您需要检查提供程序中的整个方法 class,这里是:
- (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:
,这表明此方法需要一个参数)。
您的应用程序崩溃的原因是,当您尝试执行此选择器时,您的应用程序的内部正在寻找一种名为 updateCountries
on requester
的方法,该方法接受一个参数。此方法不存在,这就是应用程序崩溃的原因。