【问题标题】:Dictionary with contents of file not working in ios带有文件内容的字典在ios中不起作用
【发布时间】:2013-12-27 10:22:23
【问题描述】:

我正在尝试从 ios 中的 plist 中获取字典。问题是代码不起作用。它在不同的函数中工作,但不是这个。

  NSDictionary* pdata=[NSDictionary dictionaryWithContentsOfFile:datapath];

该文件存在于路径中,但由于某种原因它无法正常工作。当我尝试打印字典时,我得到了 null。

编辑:这是我的路径/private/var/mobile/Applications/F4D5EFE8-58FC-4179-9492-4C4F9FD01024/Library/Preferences/abc.def.ghi.plist。这是一个越狱的应用程序,我能够成功地为另一个位置的另一个 pplist 做到这一点。

【问题讨论】:

  • 也发布您的数据路径代码。它可能有助于我们正确回答。
  • 您可以发布文件内容或您用于创建数据路径的代码吗?
  • 检查这个[链接][1],也许它可以帮助[1]:stackoverflow.com/questions/3895898/…
  • 你需要检查写的plist根文件是字典还是数组

标签: ios objective-c cocoa-touch nsdictionary jailbreak


【解决方案1】:

这应该可以正常工作。

 NSString *plistFileUrl=[[NSBundle mainBundle] pathForResource:@"someplistFile"      ofType:@"plist"];
 NSURL *url=[NSURL fileURLWithPath:plistFileUrl];
 NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:url];

【讨论】:

    【解决方案2】:

    这一定适合你。

    NSString *aStr=[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
    NSURL *aUrl=[NSURL fileURLWithPath:aStr];
    NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:aUrl];
    

    【讨论】:

    • 返回异常(*** -[NSURL initFileURLWithPath:]: nil 字符串参数)
    • 检查名称。我已经创建了 abc.def.ghi.plist 并进行了测试,它适用于我。
    【解决方案3】:

    试试这个。

    NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                                                               pathForResource:@"example" ofType:@"plist"]isDirectory:NO]];
        NSLog(@"data is %@",dict);
    

    【讨论】:

      【解决方案4】:

      试试这个

       //Local mainbundle
      NSString *path = [[NSBundle mainBundle] bundlePath];
      NSString *filePath = [path stringByAppendingPathComponent:@"Contents.plist"];
      NSMutableDictionary *dictContent = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
      NSLog(@"%@",dictContent);
      
      
        ///DocumentsDirectory
      NSMutableDictionary *dictItem=[[NSMutableDictionary alloc]init];
      [dictItem setObject:@"ravi" forKey:@"name"];
      
      NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
      NSString *documentsDirectoryPath = [paths objectAtIndex:0];
      NSString *docFilePath = [documentsDirectoryPath
                            stringByAppendingPathComponent:@"data.plist"];
      [dictItem writeToFile:docFilePath atomically: YES];
      
      NSMutableDictionary *dictContent1 = [NSMutableDictionary dictionaryWithContentsOfFile:docFilePath];
      NSLog(@"%@",dictContent1);
      

      【讨论】:

        猜你喜欢
        • 2015-02-14
        • 2013-07-02
        • 2022-06-15
        • 1970-01-01
        • 1970-01-01
        • 2017-01-10
        • 1970-01-01
        • 2014-11-18
        • 2013-09-24
        相关资源
        最近更新 更多