0

我的 AFNetworking API 响应包含一个 json 对象“place”,它可以是“restaurant”或“store”。仅当地点对象包含关键字“存储”时,我才想过滤响应以添加到 location_results 数组

这是我的 AFNetworking 请求

  [[LocationApiClient sharedInstance] getPath:@"locations.json" parameters:nil                                     success:^(AFHTTPRequestOperation *operation, id response) {
        NSLog(@"Response: %@", response);
        NSMutableArray *location_results = [NSMutableArray array];
        for (id locationDictionary in response) {
            Location *location = [[Location alloc] initWithDictionary:locationDictionary];
            [location_results addObject:location]; 
        }
        self.location_results = location_results;
        [self.tableView reloadData];
    }
                                        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            NSLog(@"Error fetching locations!");
                                            NSLog(@"%@", error);

                                        }];


}

我试着添加这个

for (id locationDictionary in response) {
    Location *location = [[Location alloc] initWithDictionary:locationDictionary];
    if([[location objectForKey:@"place"] isEqualToString:@"Store"]) // Added this line
        [location_results addObject:location];

}

但我收到一个错误 - 'Location' 没有可见的@interface 声明选择器'objectForKey'

之前如何过滤此响应[location_results addObject:location];

4

1 回答 1

0

我只能通过添加这一行来回答我的问题

self.location_results = [location_results filteredArrayUsingPredicate:
                                 [NSPredicate predicateWithFormat:@"(%K contains %@)",@"place" ,@"store"]];

  }
        self.location_results = location_results; //replaced this line
        [self.tableView reloadData];
    }
于 2013-04-14T20:18:04.950 回答