我正在尝试使用Google的地理编码API来获取城市名称和地理位置。但我收到这个错误,不知道哪个部分出了问题。有人可以帮忙吗?
这是我的代码:
的ViewController:
- (void)searchCityGeoLocationByName:(NSString *)name
{
[self.searchResults removeAllObjects];
NSString *requestUrl = [[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true&key=%@", name, GOOGLE_GEOCODING_API_KEY] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:requestUrl
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)operation.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *results = [NSMutableArray array];
for (id response in [responseObject objectForKey:@"results"]) {
self.search = [[Search alloc] initWithDictionary:response];
[results addObject:self.search];
}
self.searchResults = [NSMutableArray arrayWithCapacity:results.count];
for (int i = 0; i < results.count; i++) {
[self.searchResults addObject:results];
}
[self.searchController.searchResultsTableView reloadData];
});
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SearchCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Search *search = [self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = search.city;
return cell;
}
Search.h:
@interface Search : NSDictionary
- (id)initWithDictionary:(NSDictionary *)otherDictionary;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *lat;
@property (nonatomic, strong) NSString *lng;
@end
Search.m:
- (id)initWithDictionary:(NSDictionary *)otherDictionary
{
self = [super init];
if (self) {
self.city = [otherDictionary objectForKey:@"formatted_address"];
self.lat = [[otherDictionary valueForKeyPath:@"geometry.location"] objectForKey:@"lat"];
self.lng = [[otherDictionary valueForKeyPath:@"geometry.location"] objectForKey:@"lng"];
}
return self;
}
感谢您的帮助。
答案 0 :(得分:1)
改变这个:
for (int i = 0; i < results.count; i++) {
[self.searchResults addObject:results];
}
为:
for (int i = 0; i < results.count; i++) {
[self.searchResults addObject:results[i]];
}
您将数组添加到self.searchResults并尝试获取搜索对象。
答案 1 :(得分:1)
替换
self.searchResults = [NSMutableArray arrayWithCapacity:results.count];
for (int i = 0; i < results.count; i++) {
[self.searchResults addObject:results];
}
使用:
self.searchResults = results;
您也可以删除结果变量并直接使用self.searchResults变量。
答案 2 :(得分:0)
当您向self.searchResults
添加对象时,您每次都会添加NSMutableArray
results
,所以当您尝试将cellForRowAtIndexPath:
分配到search
时:
Search *search = [self.searchResults objectAtIndex:indexPath.row];
相反,您将返回NSMutableArray,因此在下一行中,当您尝试访问search.city
时,该对象是一个数组,而不是Search
对象。
您需要查看这些行是否正确:
for (int i = 0; i < results.count; i++) {
[self.searchResults addObject:results];
}
您确定要在其中添加results
吗?您希望NSMutableArray
包含的NSMutableArrays
是self.searchResults
吗?
您可能希望用以下内容替换它:
[self.searchResults addObject:results[i]];