iosobjective-crestkitfoursquare

Getting data from API, need to filter results


I'm working with data from a Foursquare API.

I want to get a list of coffee shops, and am getting that back correctly (I'm using RestKit)... but once I get that list, on my end I need to filter out any coffee shop that is a "Starbucks".

So right now I only know how to pull in all coffee shops, but I don't know how to parse that data once I have it before I serve it into the app table view so that there will be no Starbucks coffee shops listed.

Any ideas how I could do that? Let me know if you need any of my code snippets posted that might help.

EDIT

Normal response type from the API would be:

"venue": [{
    "name": "ABC Coffee Shop", {

So I would need to take "name" and filter out any name that was "Starbucks".


Solution

  • If FourSquare doesn't let you apply a filter to the request, to filter on the name "Starbucks" then what I would do with this is the following.

    I would start by deserializing the response into a JSON Object, which in this case will be a dictionary.

    NSError *error = nil;
    NSDictionary *responseDict = [[NSJSONSerialization JSONObjectWithData:foursquareResponse options:0 error: &error];
    NSArray *starbucks = nil;
    if (!error) {
        NSArray *coffeeShops = responseDict[@"venue"];
        starbucks = [coffeeShops filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = 'Starbucks'"]];
    } else {
        // handle the error
    }
    
    NSLog(@"Starbucks: %@", starbucks");
    

    I didn't test this code but I think it should get you on your way.