过滤AFNetworking响应

时间:2013-04-14 20:08:51

标签: ios uitableview afnetworking

我的AFNetworking API响应包含一个json对象“place”,可以是“restaurant”或“store”。我想过滤响应以添加到location_results数组,只有当place对象包含关键字“store”

这是我的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];之前过滤此响应?

1 个答案:

答案 0 :(得分:0)

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

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

  }
        self.location_results = location_results; //replaced this line
        [self.tableView reloadData];
    }