ios - 获取 '-[__NSArrayM city]: unrecognized selector sent to instance 0x111c03460' 错误

标签 ios objective-c json

我正在尝试使用 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/

相关文章:

ios - 如何使用 UIImagePNGRepresentation - iOS 压缩图像大小?

ios - NSJSONSerialization - 与 JSON 的核心数据关系

c# - 使用 JSON.Net,如果序列化 Entity Framework 对象,则禁用对数据库的访问

jquery - XirSys $.ajax() 方法返回状态 400,错误 : Could not validate application

java - Jackson转换执行时间/顺序和@JsonAnySetter

ios - UIView 从顶部添加填充

ios - 追加 RLMArray 时出现问题,更新 RLMObject 中的 RLMArray 并将其与 swift Realm 3.0 中的对象链接

objective-c - NSTableView -setDataSource 在由 FSEvents 触发时不起作用

ios - 我如何从这个关闭中取回我的数据?

ios - TableView 上下滚动几次后变慢