discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Filter array using NSPredicate


From: Koh Nyap-Hong
Subject: Re: Filter array using NSPredicate
Date: Tue, 5 Oct 2010 00:07:33 +0800

Hi Richard,

Thanks for the reply. I have modified third statement with 'description' replaces 'SELF' and removed the single quote and tested it is actually working.

Yes, basically what I want to do with NSPredicate can actually achieved using the traditionally way of while/for loop. However I was recently impressed by functional programming way of filtering array/list, I would like to try the declarative way of filtering array (I know NSPredicate way of filtering is actually just kind of declarative).

Thanks for your help again.


On Mon, Oct 4, 2010 at 4:30 PM, Richard Frith-Macdonald <address@hidden> wrote:

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]