【问题标题】:Plist not saving from dictionary (to Documents)Plist 不从字典中保存(到文档)
【发布时间】:2011-01-08 00:54:32
【问题描述】:

我一直在尝试将 NSDictionary 的 plist 保存到我的应用程序的 Documents 文件夹中。我还没有在设备上尝试过这个,但我希望它可以在模拟器上工作以进行测试。 [self createDictionaryFromChoreList] 方法只是从我的另一个类中的一些数据创建一个 NSDictionary。我几乎从网络文档中复制/粘贴了这段代码,当我去查看文件是否已保存时,我发现它没有。这是方法块。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistName = [[NSString alloc] initWithFormat:@"%@chores.plist", self.firstName];
NSString *path = [documentsDirectory stringByAppendingPathComponent:plistName];

NSDictionary *choresDictionary = [[NSDictionary alloc] initWithDictionary:[self createDictionaryFromChoreList]];
[choresDictionary writeToFile:path atomically:YES];

非常感谢任何帮助。提前致谢。

-S

【问题讨论】:

    标签: iphone save plist nsdictionary documents


    【解决方案1】:

    我也遇到了这个问题。就我而言,事实证明我使用 NSNumbers 作为键 - 这是无效的。

    【讨论】:

    • 你能详细说明为什么那无效/什么是有效的吗?
    【解决方案2】:

    这是我很快做出来的东西,它正确地将名称和公司的 plist 目录写入文档目录。我感觉您的字典创建方法可能有问题。自己尝试一下,然后添加您的代码并确保它有效。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plistDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [plistDirectory stringByAppendingPathComponent:@"userCompany.plist"];
    
    NSArray *userObjects = [[NSArray alloc] initWithObjects:@"Joe", @"Smith", @"Smith Co", nil];
    NSArray *userKeys = [[NSArray alloc] initWithObjects:@"First Name", @"Last Name", @"Company", nil];
    
    NSDictionary *userSettings = [[NSDictionary alloc] initWithObjects:userObjects forKeys:userKeys];
    
    [userSettings writeToFile:plistPath atomically:YES];
    

    【讨论】:

    • 路径是:路径:/Users/slehan/Library/Application Support/iPhone Simulator/User/Applications/6BBA0A58-E759-44C1-B018-829EBC5254AD/Documents/namechores.plist 但是当我看那里,没有文件。
    【解决方案3】:

    您还应该捕获writeToFile:atomically: 返回的 BOOL。这将告诉您写入是否成功。

    另外,您确定您正在查找正确的文档文件夹吗?如果您在模拟器中有多个应用程序,则很容易在 Finder 中打开错误的应用程序文档文件夹。我这样做过一次,这让我沮丧了几个小时。

    编辑01:

    writeToFile:atomically: 返回 false 解释了为什么不存在文件。最简单的解释是字典中的某些东西不是属性列表对象。

    来自 NSDictionary 文档:

    此方法递归地验证 所有包含的对象都是属性 列出对象(NSData 的实例, NSDate,NSNumber,NSString,NSArray, 或 NSDictionary) 在写出之前 文件,如果所有文件都返回 NO 对象不是属性列表对象, 因为生成的文件不会 有效的属性列表。

    它只需要一个非 plist 对象深埋在字典中,以防止它被转换为 plist。

    【讨论】:

    • 我将尝试捕获 writeToFile 返回的布尔值。我确实知道它是正确的 Documents 文件夹,我已经设置了一些断点并查看了对象,它们确实有正确的位置。编辑:它返回错误,我知道我正在寻找正确的文件夹,因为到目前为止我只在模拟器中运行了一个应用程序。 (不得不重新格式化)。
    【解决方案4】:

    处插入断点
    NSDictionary *choresDictionary = [[NSDictionary alloc] initWithDictionary:[self createDictionaryFromChoreList]];
    

    现在,当您离开时,将鼠标拖到 choresDictionary 上并检查工具提示,它的大小不是 0x0,或者您可以简单地对 choresDictionary 进行 NSLog 像NSLog(@"%@",choresDictionary); 我认为您的字典有 0 个键键值对,这就是您在文档文件夹中获取 null 的原因。

    谢谢,

    马杜普

    【讨论】:

      【解决方案5】:

      您究竟在哪里寻找文件?

      我有完全相同的代码,它对我来说很好。

      只是我必须深入挖掘才能获取文件。比如:

      /Users/myUserName/Library/Application Support/iPhone Simulator/User/Applications/0E62A607-8EEB-4970-B198-81CE4BDDB7AA/Documents/data.plist

      路径中的十六进制数会随着每次运行而变化。所以我每次运行都会打印文件路径。

      【讨论】:

      • 我正在寻找同样的区域;我只是不认为这是在写作。
      • 只有在每次运行时清除模拟器时,路径中的 GUID 才会更改,否则应使用相同的路径。
      【解决方案6】:

      别忘了序列化 plist 数据:

      这是我用来将信息写入 plist 的一段 sn-p 代码

      NSString *errorString;
      
      NSData *data = [NSPropertyListSerialization dataFromPropertyList:plistDict 
                                                             format:NSPropertyListXMLFormat_v1_0 
                                                    errorDescription:&errorString];
      [plistDict release];
      
      if (!data) {
        NSLog(@"error converting data: %@", errorString);
        return NO;    
      }
      
      if ([data writeToFile:[XEraseAppDelegate loadSessionPlist] atomically: YES]) {
        return YES;
      } else {
      
         NSLog(@"couldn't write to new plist");
      
       return NO;
      }
      

      【讨论】:

        【解决方案7】:

        您写的文件名是否正确: SOEMTHINGchores.plist?

        创建者:

        NSString *plistName = [[NSString alloc] initWithFormat:@"%@chores.plist", self.firstName];
        

        还有,输出是什么:

        [choresDictionary print];
        

        一些额外的信息将有助于调试。

        【讨论】:

        • 是的,没错。而且 NSDictionary 没有打印方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        相关资源
        最近更新 更多