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 10-14-2010, 10:30 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 1
Skato is on a distinguished road
Default Displaying NSdictionary variable inside NSMutableArray

Hello!

Relatively new user here, but I am hoping that will not count against me too much.

I have been trying to create a game, but I have run into a problem, visavi the scoreboard system as I am trying to use. I am using the Sparrow framework to do this, btw. Very good framework!

The coding system works by recording the profilename (NSString) and the score (NSNumber) whenever you die or complete a level. When that happens, it adds the name and score to an NSdictionary, which in turn is added to a NSMutableArray.

I did things in this way so that I could have a link between the profile name and the score, so that when I sorted the numbers, the correct names would be linked with them.

With the dictionaries, i used the following code to add the variables:

[profileNames addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Liam",@"name",[NSNumber numberWithInt:2000],@"Score",nil]];

for when I was adding example scores. When I add the proper obtained scores, i use the following:

savedData = [NSMutableDictionary dictionary];
[savedData setObject:n forKey:@"name"];
[savedData setObject:sco forKey:@"Score"];
[savedData retain];
[profileNames addObject:savedData];

both work and I have no problems with.

I also used the following code when I sorted the array 'profileNames' from highest to lowest:

NSSortDescriptor * sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"Score" ascending:NO] autorelease];
[profileNames sortUsingDescriptors:[NSMutableArray arrayWithObject:sortDescriptor]];

Saving the Array to the NSUserDefault, I could transfer the data over to the scoreboard Class. assigning the data to a new array using the NSKeyUnarchiver, things were looking up.

But then I realised that I didnt know how i could read the information linked together when I was coding to display the top 5 scores. Normally, i would use a for loop and have coding to display the information underneath, like so:

for(int i=([scoreBoard count]-1); i>=0;i--){
j++;
NSString *profileName = [scoreBoard objectAtIndex:i];

and apply the profileName string to a textfield. However, since the object at i isnt a string but a dictionary, i am in a bit of a pickle. I also didnt add any names to the dictionary's, so i cant do the simple method of calling which the books i have read on objective-c recommend i do.

So i am rather stuck. I really would appreciate anyone who would be willing to give me a hand. Thank you for reading this far at least ;-)
Skato is offline   Reply With Quote
Old 10-14-2010, 11:19 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Skato View Post
Hello!

Relatively new user here, but I am hoping that will not count against me too much.

I have been trying to create a game, but I have run into a problem, visavi the scoreboard system as I am trying to use. I am using the Sparrow framework to do this, btw. Very good framework!

The coding system works by recording the profilename (NSString) and the score (NSNumber) whenever you die or complete a level. When that happens, it adds the name and score to an NSdictionary, which in turn is added to a NSMutableArray.

I did things in this way so that I could have a link between the profile name and the score, so that when I sorted the numbers, the correct names would be linked with them.

With the dictionaries, i used the following code to add the variables:

[profileNames addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Liam",@"name",[NSNumber numberWithInt:2000],@"Score",nil]];

for when I was adding example scores. When I add the proper obtained scores, i use the following:

savedData = [NSMutableDictionary dictionary];
[savedData setObject:n forKey:@"name"];
[savedData setObject:sco forKey:@"Score"];
[savedData retain];
[profileNames addObject:savedData];

both work and I have no problems with.

I also used the following code when I sorted the array 'profileNames' from highest to lowest:

NSSortDescriptor * sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"Score" ascending:NO] autorelease];
[profileNames sortUsingDescriptors:[NSMutableArray arrayWithObject:sortDescriptor]];

Saving the Array to the NSUserDefault, I could transfer the data over to the scoreboard Class. assigning the data to a new array using the NSKeyUnarchiver, things were looking up.

But then I realised that I didnt know how i could read the information linked together when I was coding to display the top 5 scores. Normally, i would use a for loop and have coding to display the information underneath, like so:

for(int i=([scoreBoard count]-1); i>=0;i--){
j++;
NSString *profileName = [scoreBoard objectAtIndex:i];

and apply the profileName string to a textfield. However, since the object at i isnt a string but a dictionary, i am in a bit of a pickle. I also didnt add any names to the dictionary's, so i cant do the simple method of calling which the books i have read on objective-c recommend i do.

So i am rather stuck. I really would appreciate anyone who would be willing to give me a hand. Thank you for reading this far at least ;-)
You are really close. You're just missing the last bit of extracting the string from the dictionary. Rewrite your last loop like this:

Code:
for(int i=([scoreBoard count]-1); i>=0;i--)
{
  j++;
  NSDictionary* profileDict = [scoreBoard objectAtIndex: i];
  NSString *profileName = [profileDict objectForKey: @"name"];
  NSInteger profileScore = [[profileDict objectForKey: @"Score"] intValue];
  //Do whatever you need to do with the string in "profileName" and the score value
}
By the way, you have a retain in your first block of code that should not be there. I marked it in bold in your quoted post.

You create a new dictionary object "savedData" and then add it to your array of profile scores. When you add an object to an array, the array retains it. If you remove the object from the array, the array will release it. That way, the array takes care of ownership of the objects it contains. You can simply tell the object to remove an item, and the item gets released. Very neat and clean.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
display, nsdictionary, nsmutablearray, nsnumber, nsstring

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: 351
7 members and 344 guests
Desert Diva, dre, hain, HemiMG, lendo, mottdog, oceanlablight
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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