Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-25-2009, 05:51 AM   #1 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
digitaldust is on a distinguished road
Default 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
digitaldust is offline   Reply With Quote
Old 07-26-2009, 11:44 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

"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!
smasher is offline   Reply With Quote
Old 07-26-2009, 02:07 PM   #3 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
digitaldust is on a distinguished road
Default

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
digitaldust is offline   Reply With Quote
Old 07-27-2009, 05:03 AM   #4 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
digitaldust is on a distinguished road
Default

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
digitaldust is offline   Reply With Quote
Old 07-27-2009, 06:47 AM   #5 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
digitaldust is on a distinguished road
Default

I've also seen this post No NSPredicate on iPhone, How do I filter an NSMutable Array? - Mac Forums

does NSPredicate works on iphone device?

thanks,
Simone
digitaldust is offline   Reply With Quote
Old 07-27-2009, 10:36 AM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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!
smasher is offline   Reply With Quote
Old 07-27-2009, 10:54 AM   #7 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 6
digitaldust is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
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
digitaldust is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0