NSPredicate

Hi,

  1. Assume you have an NSMutableArray of NSMutableDictionary’s

  2. I have another dictionary that will be added to the array only if a particular dictionary’s key/value does not match the dictionary to be added.

  3. How is this comparison best achieved.

  4. Can it be done with NSPredicate or have I got to loop through the array extracting the dictionary for comparison.

I have achieved the comparison with a repeat loop but surely that is inefficient.

I want to check if a particular unique key/value exists in the array of dicts already and if it does then not add to the array.

I have had a look at with no luck:



set tPredicate to NSPredicate's predicateWithFormat_("transID = %@", tTransactionUniqueID)
set tExists to tPredicate's evaluateWithObject_(tRecordData)


Thanks

Terry

Hi,

NSPredicate is the best and fastest way to filter collection objects.
You should use the filteredArrayUsingPredicate: method of NSArray


set tPredicate to NSPredicate's predicateWithFormat_("transID = %@", tTransactionUniqueID)
set filteredArray to tRecordData's filteredArrayUsingPredicate_(tPredicate)
set tExists to filteredArray's |count|() > 0

StefanK,

Thanks very much, that worked perfectly.

All the best

Terry

And what is the tRecordData? NSArray? NSDictionnary? Or maybe the arranged objects from an array controller? Just curious… :slight_smile:

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Thanks!