如何阅读和阅读写入 NSArray 到 plist?

发布于 2024-12-22 15:47:21 字数 1248 浏览 2 评论 0原文

我遇到了一些关于从 plist 读取和写入 NSArray 的问题。

我在“支持文件”文件夹中创建了一个 plist 文件,我想用它在第一次加载时初始化应用程序数据。

这是我的 plist 的样子:

在此处输入图像描述

然后我使用此代码尝试将 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];
    }

然后我尝试像这样从 plist 文件加载数据,但似乎没有显示任何内容。

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

抱歉,如果这是一个简单的任务,但是我一直在查看大量教程并尝试找出如何做到这一点,但没有运气。我现在真的很沮丧——我本以为这应该是一件简单的事情。

注意: 我的 NSArray 将包含一大堆 NSDictionaries。

I'm having several issues based around reading and writing an NSArray to and from a plist.

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.

Here is what my plist looks like:

enter image description here

I then use this code to try load the plist into the app:

    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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2024-12-29 15:47:21
  1. 您需要检查 copyItemAtPath:toPath:error: 的返回值,如果该方法返回 false,至少记录错误:

    if (![fileManager copyItemAtPath:bundle toPath:filePath error:&error]) {
        NSLog(@"错误:copyItemAtPath:%@ toPath:%@错误:%@",bundle,filePath,error);
        返回;
    }
    
  2. -[NSDictionary initWithContentsOfFile:] 无法报告错误,因此如果失败,您无法轻易找出原因。尝试将文件读入 NSData 并使用 -[NSPropertyListSerialization propertyListWithData:options:format:error:] 解析它:

    NSData *data = [NSData dataWithContentsOfFile:filePath options:0 error:&error];
    如果(!数据){
        NSLog(@"错误:无法读取%@:%@",filePath,错误);
        返回;
    }
    NSMutableDictionary *savedData = [NSPropertyListSerialization propertyListWithData:数据选项:NSPropertyListMutableContainers 格式:NULL 错误:&错误];
    if (!savedData) {
        NSLog(@"错误:无法解析%@:%@",filePath,错误);
        返回;
    }
    NSMutableArray *myNSArray = [savedData objectForKey:@"KEY_Level_1"];
    保存的数据 = nil;
    如果(!myNSArray){
        NSLog(@"错误:%@:缺少 KEY_Level_1 的对象",filePath);
        返回;
    }
    

如果这样做,您将能够更容易了解为什么您的数据没有被加载。

更新

经过进一步检查,您的 plist 中的顶级字典似乎包含键“Root”。 “Root”的值是包含键“KEY_Level_1”的字典。所以你需要这样做:

NSMutableArray *myNSArray = [[savedData objectForKey:@"Root"] objectForKey:@"KEY_Level_1"];
  1. You need to check the return value of copyItemAtPath:toPath:error: and at least log the error if the method returns 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. Try reading the file into an NSData and using -[NSPropertyListSerialization propertyListWithData:options:format:error:] to parse it:

    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". The value for "Root" is a dictionary containing the key "KEY_Level_1". So you need to do this:

NSMutableArray *myNSArray = [[savedData objectForKey:@"Root"] objectForKey:@"KEY_Level_1"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文