views:

2695

answers:

2

I'm trying to implement a Save State for my iPhone App.

I've got a plist file called SaveData.plist and I can read it in via the following

NSString *pListPath2 = [bundle pathForResource:@"SaveData" ofType:@"plist"];
NSDictionary *dictionary2 = [[NSDictionary alloc] initWithContentsOfFile:pListPath2];

self.SaveData = dictionary2;
[dictionary release];

The Plist file has members

SavedGame which is a Boolean to tell the app if there really is valid data here (if they did not exit the app in the middle of a game, I don't want their to be a Restore Point.

Score which is an NSNumber. Time which is an NSNumber

Playfield which is a 16 element array of NSNumbers

How do I access those elements inside of the NSDictionary?

+4  A: 

Try [NSDictionary objectForKey:]

Where Key is the name of the member in the plist.

For example:

BOOL save = [[dictionary2 objectForKey:@"SavedGame"] boolValue];

will store the object stored in the plist for the key SavedGame into a new BOOL named save.

I imagine that your SavedGame Boolean is actually stored in the NSDictionary as an NSNumber, hence the use of boolValue to get the Boolean Value of the NSNumber.

Try this documentation: Apple NSDictionary Reference

Jeff Hellman
Always use -objectForKey: if you're calling directly — if you read the docs, -valueForKey: is used for key-value observing and Cocoa Bindings. It won't return the wrong result, but it does unnecessary work in this case.
Quinn Taylor
A: 

I'm trying to implement a Save State for my iPhone App.

Follow the rules for editing the answer or question. So, the program format will be easy to understand.
srikanth rongali