Mey
Mey

Reputation:

write and read Plist

I have the next question,

if possible to write in a .plist datas in runtime, because my app make a lot of connections and i would like to save this datas (after parsing) in a plist, for later read this datas.

Is it possible? because i'm trying and for the moment i can't.

Thanks.

Upvotes: 0

Views: 6012

Answers (2)

Dan Lorenc
Dan Lorenc

Reputation: 5394

Use this method:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

The docs explain it here: http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDictionary/writeToFile:atomically:

You just create the dictionary, then save it to the file using that method.

Upvotes: 0

Marco Mustapic
Marco Mustapic

Reputation: 3859

Yes. To save a property list:

NSString * error;
NSData * data = [NSPropertyListSerialization dataFromPropertyList:yourPlist format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
[data writeToFile:pathToYourFile atomically:YES];

yourPlist must be a kind of NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary object.

To read your property list.

NSString * error;
NSData * data = [NSData dataWithContentsOfFile:pathToYourFile];
yourPlist = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

Upvotes: 6

Related Questions