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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2011, 02:28 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
vikinara is on a distinguished road
Default NSMutableDictionary Retrieve content

Hi All

I have a NSMUtableDictionary which holds an key with a NSMutableArray of two objects in it, and another key with NSmutableArray of one object in it. How do I retrieve the contents from both the keys. Any help is appreciated.

This is what i have in log. personalvehicleDict:{
43 = (
"TempObject1",
"TempObject2"
);
56 = (
"TempObject3"
);
}

I want to retrieve data in TEmpObject1, TempObject2,TempObject3.
This is how i set the NSMutableDictionary. [personalvehiclepolicydict setObject: self.driverarray forKeyersonalvehiclepolicyattr];

Last edited by vikinara; 05-09-2011 at 03:35 PM. Reason: Edit
vikinara is offline   Reply With Quote
Old 05-09-2011, 05:21 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

At a glance,

MyObject *obj = [[personalVehicleDict objectForKey:@"43"] objectAtIndex:0];
architectpianist is offline   Reply With Quote
Old 05-09-2011, 06:57 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
vikinara is on a distinguished road
Default

Quote:
Originally Posted by architectpianist View Post
At a glance,

MyObject *obj = [[personalVehicleDict objectForKey:@"43"] objectAtIndex:0];
Thanks for the reply.

Please find my code below. My attribute string is personalvehiclepolicyattr. The value @"43" cannpt be hard coded.


[personalvehiclepolicydict setObject: self.driverarray forKeyersonalvehiclepolicyattr];

TempObject *obj = [[TempObject alloc]init];

obj = [[personalvehiclepolicydict objectForKeyersonalvehiclepolicyattr]objectAtIndex:index];

If i do this i am get data for only one object in the first array. Please help to proceed with this.
vikinara is offline   Reply With Quote
Old 05-09-2011, 07:16 PM   #4 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

Are both the keys and the objects unknown to you, do you know what the objects(value or class) will be and not the keys, or do you know what the keys will be and not the objects?

If you want to get all the objects out, use -allKeys(NSDictionary Method), put it in a for loop, and then -objectForKey:[allKeys objectAtIndex:x] and then additional code as need.
__________________

Last edited by iisword; 05-09-2011 at 07:24 PM.
iisword is offline   Reply With Quote
Old 05-09-2011, 07:19 PM   #5 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

You need to use :

Code:
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
Loading…
iSDK is offline   Reply With Quote
Old 05-09-2011, 07:29 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
vikinara is on a distinguished road
Default

Quote:
Originally Posted by iisword View Post
Are both the keys and the objects unknown to you, do you know what the objects(value or class) will be and not the keys, or do you know what the keys will be and not the objects?

If you want to get all the objects out, use -allKeys(NSDictionary Method), put it in a for loop, and then -objectForKey:[allKeys objectAtIndex:x] and then additional code as need.

I dont know what the keys will be or what the objects will be. Both are dynamic. Please help me with this.
vikinara is offline   Reply With Quote
Old 05-09-2011, 08:14 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
vikinara is on a distinguished road
Default

Quote:
Originally Posted by iSDK View Post
You need to use :

Code:
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
Loading…
Thank u for the reply. But I am not exactly sure how to use this piece of code. Can u please help me with some example.

Last edited by vikinara; 05-09-2011 at 08:24 PM.
vikinara is offline   Reply With Quote
Old 05-09-2011, 10:13 PM   #8 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

Well I'm going to assume the dictionary has only NSMutableArrays in it(if not, you need to enumerate with
Code:
while(NSMutableArray *array in [dictionary objectEnumerator])
{
   for (int x = 0; x < [array count]; x++)
   {
       //do stuff
    }
}
)

All you have to do is get the keys ([dictionary allKeys]) then it's basicly a for loop within a for loop that does something with the object.

Note when making NSDictionaries, you should know at the minimum the class for object you just added so you can retrieve it and do something with it.

If you want iSDK way, look at this link where it shows an example of a block.
__________________

Last edited by iisword; 05-09-2011 at 10:19 PM.
iisword is offline   Reply With Quote
Old 05-10-2011, 04:33 AM   #9 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Code:
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
  NSLog (@"Key: %@ for value: %@", key, object);
}];
or

Code:
id key, object;
 
NSArray *keys = [dict allKeys];
for (int i = 0; i <  [keys count] ; i++)
{
  key = [keys objectAtIndex: i];
  object = [dict objectForKey: key];
  NSLog (@"Key: %@ for value: %@", key, object);
}
__________________
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutabledictionary

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: 350
9 members and 341 guests
dansparrow, iOS.Lover, lorrettaui53, MikaelBartlett, oztemel, pbart, PlutoPrime, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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