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:
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:
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 ;-)
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:
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:
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.
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.