简体   繁体   English

如何读写NSArray到plist?

[英]How to read & write to an NSArray to plist?

I'm having several issues based around reading and writing an NSArray to and from a plist. 我在与plist之间读写NSArray遇到了一些问题。

I have created a plist file in the 'Supporting Files' folder which I want to use to initialise the app data with upon the first load. 我已经在“ Supporting Files”文件夹中创建了一个plist文件,该文件用于在首次加载时用于初始化应用程序数据。

Here is what my plist looks like: 这是我的plist的样子:

在此处输入图片说明

I then use this code to try load the plist into the app: 然后,我使用以下代码尝试将plist加载到应用中:

    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePath = [documentsDirectory stringByAppendingPathComponent:kDataFile];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath])
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:filePath error:&error];
    }

I then try to load the data from the plist file like so, however nothing seems to be displayed. 然后,我尝试像这样从plist文件加载数据,但是似乎什么也没有显示。

NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSMutableArray *myNSArray = [[savedData objectForKey:@"KEY_Level_1"] mutableCopy];
savedData = nil;

Sorry if this is a simple task, however I've been looking at lots of tutorials and trying to work out how to do this with no luck. 抱歉,如果这是一个简单的任务,但是我一直在看很多教程,并尝试找出如何做到这一点。 I'm getting really frustrated now - I would have thought it should be a simple thing to do. 我现在真的很沮丧-我本以为这应该很简单。

NOTE: My NSArray will contain a whole bunch of NSDictionaries. 注意:我的NSArray将包含一大堆NSDictionaries。

  1. You need to check the return value of copyItemAtPath:toPath:error: and at least log the error if the method returns false: 您需要检查copyItemAtPath:toPath:error:的返回值,如果该方法返回false,至少要记录该错误:

     if (![fileManager copyItemAtPath:bundle toPath:filePath error:&error]) { NSLog(@"error: copyItemAtPath:%@ toPath:%@ error:%@", bundle, filePath, error); return; } 
  2. -[NSDictionary initWithContentsOfFile:] has no way to report errors, so if it's failing, you cannot easily figure out why. -[NSDictionary initWithContentsOfFile:]无法报告错误,因此,如果失败,则无法轻松找出原因。 Try reading the file into an NSData and using -[NSPropertyListSerialization propertyListWithData:options:format:error:] to parse it: 尝试将文件读入NSData并使用-[NSPropertyListSerialization propertyListWithData:options:format:error:]进行解析:

     NSData *data = [NSData dataWithContentsOfFile:filePath options:0 error:&error]; if (!data) { NSLog(@"error: could not read %@: %@", filePath, error); return; } NSMutableDictionary *savedData = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:&error]; if (!savedData) { NSLog(@"error: could not parse %@: %@", filePath, error); return; } NSMutableArray *myNSArray = [savedData objectForKey:@"KEY_Level_1"]; savedData = nil; if (!myNSArray) { NSLog(@"error: %@: object for KEY_Level_1 missing", filePath); return; } 

If you do this, you'll be able to more easily see why your data is not being loaded. 如果这样做,您将可以更轻松地了解为什么未加载数据。

UPDATE 更新

On further inspection, it looks like the top-level dictionary in your plist contains the key "Root". 经过进一步检查,您的plist中的顶级词典看起来包含键“ Root”。 The value for "Root" is a dictionary containing the key "KEY_Level_1". “ Root”的值是包含键“ KEY_Level_1”的字典。 So you need to do this: 因此,您需要这样做:

NSMutableArray *myNSArray = [[savedData objectForKey:@"Root"] objectForKey:@"KEY_Level_1"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM