Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 495
K2Digital Avatar asked Jul 01 '09 03:07

K2Digital


People also ask

How do I parse a plist file?

To write out and to parse a plist file, use the dump() and load() functions. To work with plist data in bytes objects, use dumps() and loads() . Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys), bytes , bytearray or datetime.

How do I read Apple plist files?

Launch Finder, click Macintosh HD under Locations. Next, input '. plist' in the searching box on the right top of the window, all the plist files will be listed as follows. Then you can open and edit the plist file in macOS by using Xcode or Apple Property List Editor.

How do I read a plist file in Swiftui?

Property lists are perfect for saving simple, static key-value data in your app. Here's how you can read a plist with Swift: func getPlist(withName name: String) -> [String]?


2 Answers

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.

like image 102
Jeff Hellman Avatar answered Oct 17 '22 08:10

Jeff Hellman


For Setting Bool Value You can Use :

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

For Getting Bool value you can use :

[Dictionary boolForKey:@"preference"];

like image 40
Shahid Aslam Avatar answered Oct 17 '22 08:10

Shahid Aslam