Friday, May 27, 2022

#1 2010-12-10 01:32:13 pm

tellboy
Member
Registered: 2005-06-23
Posts: 155

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:

Applescript:



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

Thanks

Terry

Offline

 

#2 2010-12-10 01:43:16 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11766
Website

Re: NSPredicate

Hi,

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

Applescript:


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


regards

Stefan

Offline

 

#3 2010-12-11 04:13:12 am

tellboy
Member
Registered: 2005-06-23
Posts: 155

Re: NSPredicate

StefanK,

Thanks very much, that worked perfectly.


All the best

Terry

Offline

 

#4 2010-12-11 12:57:39 pm

leonsimard
Member
From:: Montreal, Canada
Registered: 2009-09-27
Posts: 531

Re: NSPredicate

And what is the tRecordData? NSArray? NSDictionnary? Or maybe the arranged objects from an array controller? Just curious... smile

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

Offline

 

#5 2010-12-11 12:58:52 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11766
Website

Re: NSPredicate

tellboy wrote:

1. Assume you have an NSMutableArray of NSMutableDictionary's


regards

Stefan

Offline

 

#6 2010-12-11 12:59:30 pm

leonsimard
Member
From:: Montreal, Canada
Registered: 2009-09-27
Posts: 531

Re: NSPredicate

Thanks!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)