如何在另一个viewcontroller中访问我的plist?

时间:2014-08-09 21:53:19

标签: ios objective-c plist

我有一个3个数组,我试图在一个视图中保存为3个不同的plist,然后将该数据加载到另一个视图中的数组中。我无法弄清楚如何在我的其他视图中访问plist中的数据并在初始视图中正确保存它。这是我第一次看到的代码。

NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentsDirectory1 = [documentPaths objectAtIndex:1];
    NSString *documentsDirectory2 = [documentPaths objectAtIndex:2];
    NSString *documentsDirectory3 = [documentPaths objectAtIndex:3];
    NSString *documentPlistPath1 = [documentsDirectory stringByAppendingPathComponent:@"balance.plist"];
    NSString *documentsPlistPath2 = [documentsDirectory1 stringByAppendingPathComponent:@"principal.plist"];
    NSString *documentsPlistPath3 = [documentsDirectory2 stringByAppendingPathComponent:@"interest.plist"];
    NSString *documentsPlistpath4 = [documentsDirectory3 stringByAppendingPathComponent:@"dates.plist"];
    [dateValues writeToFile:documentsPlistpath4 atomically:YES];
    [balanceLabels writeToFile:documentPlistPath1 atomically:YES];

    [pricipalLabels writeToFile:documentsPlistPath2 atomically:YES];

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

创建一个数组

NSArray *array = @[@"abc", @"cba"];

获取plist文件的路径

NSString *documentsFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = @"my_plist_file";
NSString *filePath = [documentsFolderPath stringByAppendingPathComponent:fileName];

将数组保存到.plist

[array writeToFile:filePath atomically:YES];

从.plist

读取数组
NSArray *arrayFromDisk = [NSArray arrayWithContentsOfFile:filePath];