【问题标题】:Loading Array From .Plist从 .Plist 加载数组
【发布时间】:2011-10-22 01:33:04
【问题描述】:

我正在尝试从 .Plist 中的数组加载我的数组,但它不起作用。

plist 如下所示:

这是我正在使用的代码:

NSString *path = [[NSBundle mainBundle]pathForResource:@"DiseasePropertyList" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.myArray = rootLevel;
[rootLevel release];

【问题讨论】:

  • 你看起来很危险!

标签: iphone objective-c xcode nsarray plist


【解决方案1】:

试试这个。请更改文件名。 它工作正常。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *recentFilePath = [documentsDirectory stringByAppendingPathComponent:@"recent.plist"];
NSArray *history = [NSArray arrayWithContentsOfFile:recentFilePath];

【讨论】:

  • 另一种方式:` NSURL* url = [[NSBundle mainBundle] URLForResource:@"DiseasePropertyList" withExtension:@"plist"]; NSArray* myArray = [NSArray arrayWithContentsOfURL: url];`
【解决方案2】:

您的 plist 实际上是一本字典。该数组是键“Array”的字典的对象。您可以在 Xcode 4 中通过选择“Array”和剪切 (CMD-x) 将 plist 制作成数组,然后粘贴到空 plist (CMD v) 中。

【讨论】:

  • 我会说这很可能是问题所在。您还可以将 plist 视为源,然后从文件中删除 <dict></dict> 标记(我一直这样做)。
  • 在开始标签 .. 之后也去掉 .. 标签.. 这肯定会为我解决问题....
【解决方案3】:
NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"]; 
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];

只需检查 settingsDict 的描述。希望对您有所帮助。

【讨论】:

    【解决方案4】:

    如果您的 plist 文件在您的应用程序包中,最简单的是:

    NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]];
    

    【讨论】:

      【解决方案5】:

      使用这种方法对我来说效果很好。

      NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-02
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        相关资源
        最近更新 更多