Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'd like to serialize a bunch of data and save it to a file, then be able to load it (naturally) and play it back with a feature I have written. (Sorry if my terminology is off, I am a novice with this sort of thing.) What is the best way to do this with iOS? From looking at documentation here:

Standard Application Behaviors Guide

I've gathered that I should use NSSearchPathForDirectoriesInDomains for finding the appropriate storage directory root (Documents?) and then use NSData to store these data buffers I create and write them to file. Am I spot on with that or have I got it wrong? Any other sage advice?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
395 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use plist.
It is very easy to save list of string or other data without using core data.
See Property List Programming Guide

For example, this code will save an object (rootObj) which can be an array or a dictionary:

NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"yourFile.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj
                                                               format:NSPropertyListXMLFormat_v1_0
                                                     errorDescription:&error];
if(plistData) {
  [plistData writeToFile:plistPath atomically:YES];
}
else {
  NSLog(@"Error : %@",error);
  [error release];
}

There is some limitation on supported class (see the guide)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...