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 > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 01-22-2011, 04:18 PM   #1 (permalink)
r@t
Registered Member
 
Join Date: Jan 2011
Posts: 21
r@t is on a distinguished road
Default NSMutableArray of names from NSDictionaries in an NSDictionary

Hi, I am having trouble making an array of objects (employee names) from a group of NSDictionaries (individual employee records) inside another NSDictionary *dictionary (personnel). The key for each sub Dictionary is an employee ID.

I might well be coming at this from a completely wrong direction, as i am very much a newbie, but i have got as far as getting a pName from each sub Dictionary, all be it fleetingly. I just can't get the pNames added into MutableArray *names.

any help or guidance would be greatly appreciated ...

Code:
                NSMutableArray *names;
                NSArray *keys;
		int i, count;
		id key, value;
		
		keys = [dictionary allKeys];
		count = [keys count];
		for (i = 0; i < count; i++)
		{
			key = [keys objectAtIndex: i];
			value = [dictionary objectForKey: key];
			NSLog (@"value:%@", value); 
				
			NSString *pName = [value objectForKey:@"personName"];
			NSLog (@"pName:%@", pName);// the debugger shows the correct pName string with each loop
				
			[names addObject:pName];
		}
			NSLog (@"names:%@", names);// in the debugger names (null)
r@t is offline   Reply With Quote
Old 01-22-2011, 06:29 PM   #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
Thumbs up

I don't see you pointing the names pointer at an NSMutableArray - you declared the pointer variable but never created an object for it to point to

Code:
                NSMutableArray *names;
                NSArray *keys;
		int i, count;
		id key, value;
		
                names = [NSMutableArray array]; //setting names here
		keys = [dictionary allKeys];
		count = [keys count];
		for (i = 0; i < count; i++)
		{
			key = [keys objectAtIndex: i];
			value = [dictionary objectForKey: key];
			NSLog (@"value:%@", value); 
				
			NSString *pName = [value objectForKey:@"personName"];
			NSLog (@"pName:%@", pName);// the debugger shows the correct pName string with each loop
				
			[names addObject:pName];
		}
	NSLog (@"names:%@", names);
Now it should work, but I'll go you two better. First with some fast enumeration:
Code:
                NSMutableArray *names = [NSMutableArray array];        
		NSArray *values = [dictionary allValues];
		for (id value in values)
		{
			NSString *pName = [value objectForKey:@"personName"];
			[names addObject:pName];
		}
	NSLog (@"names:%@", names);
Or better yet, some key-value coding:
Code:
        
	NSArray *values = [dictionary allValues];
		
	NSArray *names = [values valueForKey:@"personName"];

	NSLog (@"names:%@", names);
Names is not mutable in the last example, but you could call mutableCopy on it if you need to.

-- Society for the Elimination of most Loops
__________________

Free Games!

Last edited by smasher; 01-23-2011 at 10:25 AM.
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
nsdictionary, nsmutablearray

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: 411
11 members and 400 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, QuantumDoja, Retouchable, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,924
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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