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?
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.
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.
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]?
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.
For Setting Bool Value You can Use :
[Dictionary setBool:TRUE forKey:@"preference"];
For Getting Bool value you can use :
[Dictionary boolForKey:@"preference"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With