Storing and Retrieving from a Plist [duplicate]

白昼怎懂夜的黑 提交于 2019-12-03 16:35:05
Lolloz89

You can store the following data types in a .plist file as value:

  • NSArray
  • NSMutableArray
  • NSDictionary
  • NSMutableDictionary
  • NSData
  • NSMutableData
  • NSString
  • NSMutableString
  • NSNumber
  • NSDate

With an NSDictionary you can store in a plist an associative array composed by some key-value pair. Use the NSArray if you only want to save a data series.

To save one of the object above on a plist file simply write:

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"yourFileNameHere"];
}

 //Write to the plist

 [myArray writeToFile:[self dataFilePath] atomically:YES];

 //Read from the plist

 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
     NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!