13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3: Foundational APIs 73Property ListsProperty lists are a hierarchy of key-value pairs stored in XML format. They can be serialized,creating an NSData instance that is writable to disk.Listing 3-50. Property ListsNSArray *stringArray = [NSArray arrayWithObjects:@"some string", @"another string", nil];NSString *error = nil;NSString *path = [[NSBundle mainBundle] pathForResource:@"Strings" ofType:@"plist"];id plistData = [NSPropertyListSerialization dataFromPropertyList:stringArrayformat:NSPropertyListXMLFormat_v1_0errorDescription:&error];if (plistData) //short hand for if (plistData != nil){[plistData writeToFile:path atomically:YES];}else{NSLog(@"Could not create plist data: %@", error);}The code in Listing 3-50 creates an NSData object and persists it to disk in the main applicationbundle as the file Strings.plist. In the case of an error during serialization, the NSError objectis logged.The contents of the actual file written to disk (see Listing 3-51 for the example output) don’ttypically matter to you as a developer; while it is possible to edit the XML file directly, in code onlythe Foundation framework should be used to read or write the file to disk. We know you’re eager towrite your own XML parser, but just use the Foundation framework and save yourself a lot of time.Listing 3-51. A Property List Filesome stringanother stringAccessing a property list that has already been written to disk looks very similar (see Listing 3-52).Listing 3-52. Property ListsNSString *error = nil;NSString *path = [[NSBundle mainBundle] pathForResource:@"Strings" ofType:@"plist"];NSData *plistData = [NSData dataWithContentsOfFile:path];NSPropertyListFormat format;id plist;www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!