Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pList in iOS Programming

Recently, I'm a senior in high school, and I'm interested in making apps for iPhone. Recently, one of my apps came out: NBlock. It's a puzzle app and it's very challenging. However, it has a few problems. The high scores are not saved. I've been told to use a plist. Any tips?

like image 908
Andrew Avatar asked Aug 14 '11 21:08

Andrew


People also ask

What is the use of plist in iOS?

plist file contains critical information about the configuration of an iOS mobile app—such as iOS versions that are supported and device compatibility—which the operating system uses to interact with the app. This file is automatically created when the mobile app is compiled.

What is plist file Xcode?

Xcode supplies an information property list file when you create a project from a template, as described in Create a project. By default, Xcode names this file Info. plist and adds it to your project as a source file that you can edit. Xcode creates one information property list for each target in the project folder.

What is the use of plist in Swift?

What is a plist file? A plist or property list is an XML file containing data in form of key-value pairs. Just line a dictionary in the Swift, so it's a list of values associated with keys. In Xcode, you can open the plist's XML structure by right-clicking on a property list file, then choosing Open As → Source Code.


2 Answers

The URL based method for this:

// Get the URL for the document directory
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *documentDirectoryURL = [[fileManager URLsForDocumentDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];

// Turn the filename into a string safe for use in a URL
NSString *safeString = [@"scores.plist" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Create an array for the score
NSMutableArray *array = [[NSMutableArray alloc] init];      
[array addObject:[NSNumber numberWithInt:score]];

// Write this array to a URL
NSURL *arrayURL = [NSURL URLWithString:safeString relativeToURL:documentDirectoryURL];
[array writeToURL:arrayURL atomically:YES];
like image 87
Abizern Avatar answered Oct 06 '22 03:10

Abizern


I'd avoid using a plist. The easiest way to save simple data in an application, by far, is NSUserDefaults.

Check out this tutorial for a simple guide on how to use NSUserDefaults. Always be sure to synchronize NSUserDefaults when you're done writing to them.

If you're looking for a more powerful (but more complex) way to save data, check out Apple's guide to using Core Data.

like image 29
mopsled Avatar answered Oct 06 '22 03:10

mopsled