07-25-2009, 05:51 AM
#1 (permalink )
New Member
Join Date: Jul 2009
Posts: 6
newbie question on array subsets
Hello List,
I have a simple question, but I cannot solve it. Suppose you have a NSMutableArray like in TheElements example app. You fill it with
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Elements" ofType:@"plist"];
NSMutableArray *newArray = [[NSArray alloc] initWithContentsOfFile:thePath];
suppose you want to subset the array using only the elements in the plist with state = Solid
what is the right sintax to do that?
many thanks,
simone
07-26-2009, 11:44 AM
#2 (permalink )
Registered Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
"filteredArrayUsingPredicate" will do that for you:
NSArray Class Reference
Otherwise you could loop through the original array, and add just the items that pass your test to a new array.
__________________
Free Games!
07-26-2009, 02:07 PM
#3 (permalink )
New Member
Join Date: Jul 2009
Posts: 6
well I was thinking about something like
NSArray *filteredElementArray = [newArray valueForKey:@"state"];
but I didn't know how to specify the condition state = Solid
I'll try to use filteredArrayUsingPredicate, as soon as I understand its sintax...
many thanks for your reply,
simone
07-27-2009, 05:03 AM
#4 (permalink )
New Member
Join Date: Jul 2009
Posts: 6
I am trying this, but it doesn't seem to work properly:
// read the element data from the plist
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Elements" ofType:@"plist"];
// build an array with the plist
NSMutableArray *newArray = [[NSArray alloc] initWithContentsOfFile:thePath];
// build a predicate to filter the array
NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'Solid'"];
// filter the array with the predicate
NSArray *rawJewelsArray =[newArray filteredArrayUsingPredicate:Predicate];
Where am I doing something wrong? I suppose SELF is not the right keyword, but how to specify the "state" condition?
best regards,
simone
07-27-2009, 10:36 AM
#6 (permalink )
Registered Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Ah, I'm very sorry. I didn't try NSPredicate, and didn't know it's missing on the iPhone.
You'll have to build a second array instead:
Code:
NSMutableArray *filteredArray = [[NSMutableArray alloc] init]
for (id item in rawJewelsArray){
if (item.solid)
[filteredArray addObject: item];
}
You can replace "id" with "Jewel *" or the real type of your objects.
__________________
Free Games!
07-27-2009, 10:54 AM
#7 (permalink )
New Member
Join Date: Jul 2009
Posts: 6
Quote:
Originally Posted by
smasher
Ah, I'm very sorry. I didn't try NSPredicate, and didn't know it's missing on the iPhone.
You'll have to build a second array instead:
Code:
NSMutableArray *filteredArray = [[NSMutableArray alloc] init]
for (id item in rawJewelsArray){
if (item.solid)
[filteredArray addObject: item];
}
You can replace "id" with "Jewel *" or the real type of your objects.
I see... many thanks for the code example!!!
best regards,
Simone
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 456
16 members and 440 guests
Domele , Duncan C , Feldspar , karatebasker , MacBook MH , Objective Zero , patapple , Paul Slocum , peterwilli , pipposanta , PixelInteractive , Punkjumper , rubyeim54 , SLIC , taylor202 , Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli