discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Filter array using NSPredicate


From: Richard Frith-Macdonald
Subject: Re: Filter array using NSPredicate
Date: Mon, 4 Oct 2010 09:30:44 +0100

On 3 Oct 2010, at 14:29, Koh Nyap-Hong wrote:

> Hi List,
> 
> I with to filter array using NSPredicate (seems a lot more easier than  while 
> loop with NSEnumerator), after read through some cocoa examples online, I run 
> a simple test using the following codes:
> 
>  NSArray *pitches = [NSArray arrayWithObjects: @"Do", @"Re", @"Mi", @"Fa", 
> @"So", @"La", nil];
> 
>   NSArray *filteredWithSELF = [pitches filteredArrayUsingPredicate: 
> [NSPredicate predicateWithFormat: @"SELF == 'Do'"]];  
>   NSArray *filteredWithDescription = [pitches filteredArrayUsingPredicate: 
> [NSPredicate predicateWithFormat: @"description == 'Do'"]];
>   NSArray *filteredWithFormat = [pitches filteredArrayUsingPredicate: 
> [NSPredicate predicateWithFormat: @"SELF == '%@'", @"Do"]];
> 
> Out of the three predicate statements, however,  the first and last one 
> return me empty array. Only the second one returns an array containing 1 
> element which is the @"Do" string.
> 
> Did I make any mistake in the codes, or this feature is not fully implemented 
> yet in base-1.20?  

Both ...

Firstly the use of SELF is not well implemented (though you could try the 
version of the base library from svn trunk).
In general NSPredicate is not fully implemented because people don't seem to 
use it and contribute improvements.  Perhaps because the more basic features 
are all that most people need?

Secondly, your use of quotes in the third expression is incorrect ... the text 
'%@' in the format string would be parsed as equivalent to @"'Do'" .





reply via email to

[Prev in Thread] Current Thread [Next in Thread]