K2Digital
K2Digital

Reputation: 603

Reading data from a plist file

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?

Upvotes: 16

Views: 22158

Answers (2)

Jeff Hellman
Jeff Hellman

Reputation: 2117

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.

Upvotes: 26

Shahid Aslam
Shahid Aslam

Reputation: 2585

For Setting Bool Value You can Use :

[Dictionary setBool:TRUE forKey:@"preference"];

For Getting Bool value you can use :

[Dictionary boolForKey:@"preference"];

Upvotes: 2

Related Questions