What are the options for saving data in iOS?

前端 未结 3 1516
旧时难觅i
旧时难觅i 2020-11-30 02:20

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 i

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 02:47

    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)

提交回复
热议问题