我是一名新手,并设计了一个天气预报应用程序作为我在iOS的第一个项目。
我设计了UISearchBar
来搜索城市名称。搜索栏的工作方式如下:
1)第1步:输入" Naji"。它在表格视图中显示两个城市: 纳吉 纳吉
2)第2步:键入" Najib"。它没有显示任何内容,因为结果为null。
(错误部分)
3)第3步:每当我输入" Najib"时,应用终止时会出现以下错误:
***因未捕获的异常终止应用' NSInvalidArgumentException',原因:' - [NSTaggedPointerString count]: 无法识别的选择器发送到实例0xa006162696a614e6'
代码是:
-(void)searchBar:(UISearchBar *)asearchBar textDidChange:(NSString *)searchText{
if ([searchText length] <= return;
if ([searchText length]==0) {
[discityname removeAllObjects];
[discityname addObjectsFromArray:cityname];
}else{
[discityname removeAllObjects];
NSURLSession *session=[NSURLSession sharedSession];
NSString *complete_url=[NSString stringWithFormat:@"https://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20geo.places%%20where%%20text%%3D%%22%@%%25%%22&format=json&diagnostics=true&callback=",searchText];
NSURLSessionDataTask *dataTask=[session dataTaskWithURL:[NSURL URLWithString:complete_url]completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",json);
NSMutableDictionary *currentDict = [[NSMutableDictionary alloc]initWithDictionary:json[@"query"]];
int a=[[currentDict valueForKey:@"count"] intValue];
if (a!=0) {
cityname=[[NSMutableArray alloc]init];
// if ([currentDict valueForKey: @"results"] != [NSNull null])
// {
NSMutableDictionary *conDict = [[NSMutableDictionary alloc]initWithDictionary:currentDict[@"results"]];
NSMutableArray *places=[conDict valueForKey:@"place"];
NSMutableArray *country=[places valueForKey:@"country"];
NSMutableArray *countryname=[country valueForKey:@"content"];
NSMutableArray *placeName= [places valueForKey:@"name"];
NSMutableArray *centroid=[places valueForKey:@"centroid"];
NSMutableArray *latitude=[centroid valueForKey:@"latitude"];
NSMutableArray *longitude=[centroid valueForKey:@"longitude"];
//if ([placeName isKindOfClass:[NSMutableArray class]]) {
//}
//cityname=[[NSMutableArray alloc]initWithArray:placeName];
NSLog(@"%lu",placeName.count);
for (int i=0; i<placeName.count; i++) {
[cityname addObject:[placeName objectAtIndex:i]];
[countrynames addObject:[countryname objectAtIndex:i]];
}
dispatch_async(dispatch_get_main_queue(), ^{
discityname=[[NSMutableArray alloc]initWithArray:cityname];
discountryname=[[NSMutableArray alloc]initWithArray:countrynames];
latitudenames=[[NSMutableArray alloc]initWithArray:latitude];
longitudenames=[[NSMutableArray alloc]initWithArray:longitude];
[tableView reloadData];
});
}
// for(int i=0;i<[placeName count];i++)
//i NSLog(@"The place is: %@ , %@, %@, %@",placeName[i],countryname[i],latitude[i],longitude[i]);
}];
[dataTask resume];
for (NSString *string in discityname) {
NSRange r=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location!=NSNotFound) {
[discityname addObject:string];
}
}
}
[tableView reloadData];
}
答案:它应该显示城市名称&#34; Najib&#34;在json中返回。