我正在尝试使用 Google 的地理编码 API 来获取城市名称和地理位置。但是我遇到了这个错误,不知道我哪里出错了。有人可以帮忙吗?
这是我的代码:
View Controller :
- (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;
}
搜索.h:
@interface Search : NSDictionary
- (id)initWithDictionary:(NSDictionary *)otherDictionary;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *lat;
@property (nonatomic, strong) NSString *lng;
@end
搜索.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;
}
感谢您的帮助。
最佳答案
改变这个:
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 并尝试获取 Search 对象。
关于ios - 获取 '-[__NSArrayM city]: unrecognized selector sent to instance 0x111c03460' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23137493/