2

Consider the following JSON object:

[
  {
  "name":"joe",
  "place":"here",
  "type":[
     "abc",
     "cde",
     "efg"
     ]
  },
  {
  "name":"ian",
  "place":"somewhere",
  "type":[
     "c",
     "ddd",
     "eee"
     ]
  },
  {
  "name":"mike",
  "place":"there",
  "type":[
     "any",
     "place",
     "nice"
     ]
  },
]

How would I create a predicate in Objective-C to query the values of the different "type" objects and filter based on that

Thanks

2 Answers 2

2

Create predicate with block, and within block create your compare function.

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary * bindings) {
  if([[evaluatedObject objectForKey:@"name"] isEqual:@"SomeString]) {
     return YES;
  } else {
     return NO;
  }
}

This simple first level comparing, but you get a point. From this object you can go deeper and return YES/NO at point you can determine compare results.

Sign up to request clarification or add additional context in comments.

Comments

2

If your data is in an NSArray named jsonArray, you can filter on type with:

NSString *typeToSelect = @"abc";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY type like %@",typeToSelect];
NSArray *filteredArray = [jsonArray filteredArrayUsingPredicate:predicate];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.