【问题标题】:array from plist iOS来自 plist iOS 的数组
【发布时间】:2013-11-17 03:39:32
【问题描述】:

我正在尝试访问我尝试从 plist 创建的数组。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =  [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"methodologyQuestions.plist"];
    NSMutableArray *myArray = [[[NSMutableArray alloc] initWithContentsOfFile:plistPath]mutableCopy];
    NSLog(@"%@", [myArray objectAtIndex:0]);

这是我的 plist 方法Questions.plist 的快照

问题是当我记录数组时它是空的。我做错了什么?

【问题讨论】:

  • 您是否验证过所有其他变量都不为 NULL? paths 是否返回有效数组? documentsDirector 是否返回有效路径?等
  • 当我 NSLog 路径和文档目录 2013-11-16 22:49:19.252 GlobalHistoryRegents[74153:70b] 文档目录是 /Users/myname/Library/Application Support/ iPhone Simulator/7.0.3/Applications/896EF347-A35E-40E2-9BE1-8CFAC5303347/Documents 2013-11-16 22:49:19.252 GlobalHistoryRegents[74153:70b] plistPath 是 /Users/myname/Library/Application Support/iPhone Simulator/7.0.3/Applications/896EF347-A35E-40E2-9BE1-8CFAC5303347/Documents/methodologyQuestions.plist 2013-11-16 22:49:19.253 GlobalHistoryRegents[74153:70b] (null)
  • 抱歉,如果不清楚。所以它显然找到了一个路径和documentsDirectory,但我怎么能确定它实际上是在寻找methodologyQuestions.plist?
  • 是的,这很奇怪。如果您不制作可变副本会发生什么?仅使用文件内容进行分配和初始化是否有效?
  • 我发现this answer 可能会有所帮助。看来您可能需要将其读入字典并将数组从中提取出来?

标签: ios null nsarray plist


【解决方案1】:

问题是您从错误的路径读取。如果这是与您的应用捆绑在一起的文件,则它不在 Documents 文件夹中,而是在应用的资源包中。

路径是:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"methodologyQuestions" ofType:@"plist"];

【讨论】:

    【解决方案2】:

    试试这个,

    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    NSArray * myArray =dictionary(@"Root");
    

    【讨论】:

      【解决方案3】:

      plist 在捆绑包中,而不是在文档目录中。

      NSString *file = [[NSBundle mainBundle] pathForResource:@"methodologyQuestions" ofType:@"plist"];
          NSArray * arr = [NSArray arrayWithContentsOfFile:file];
          NSLog(@"%i", arr.count);
      

      如果我知道我在做什么,这应该是显而易见的,但是,唉,希望这将有助于将来尝试访问他们的 plist 的人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多