【问题标题】:access to plist in app bundle returns null访问 app bundle 中的 plist 返回 null
【发布时间】:2011-10-20 14:06:53
【问题描述】:

我正在使用我之前使用过的一段代码成功地将 plist 加载到数组中:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

initWithContentsOf 文件失败(数组为空,表明它在应用程序包中找不到 plist)。

在 iPhone 模拟器下,我检查了第一行创建的路径是否指向应用文件(确实如此),并使用 Finder 上下文菜单“显示包内容”向自己证明“species_name.plist”实际上是在捆绑包中——长度表明它包含东西,所以它应该工作(并且在我编写的其他 iOS 应用程序中也有)。欢迎提出建议...

[环境 Xcode 4.2 测试版,iOS 4.3.2]。

【问题讨论】:

    标签: objective-c ios plist nsbundle


    【解决方案1】:

    您应该使用NSDictionary 实例的initWithContentsOfFile: 方法,而不是NSArray。 这是示例:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    NSArray *array = [NSArray arrayWithArray:[dict objectForKey:@"Root"]];
    

    或者这个:

    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString    *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
    NSData      *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization                     
                                              propertyListFromData:plistXML
    mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                              format:&format
                                              errorDescription:&errorDesc];
    NSArray *array = [NSArray arrayWithArray:[temp objectForKey:@"Root"]];
    


    编辑

    您可以将NSArrayinitWithContentsOfFile: 方法与使用NSArraywriteToFile:atomically: 方法创建的文件一起使用。使用 writeToFile:atomically: 创建的 Plist 具有以下结构:

    <plist version="1.0">
    <array>
        ...Content...
    </array>
    </plist>
    

    在 XCode 中创建的 Plist 具有这种结构:

    <plist version="1.0">
    <dict>
        ...Content...
    </dict>
    </plist>
    

    这就是为什么NSArray dosnt 的initWithContentsOfFile: 方法起作用的原因。

    【讨论】:

      【解决方案2】:

      这种类型的plist

      OR(基本上都是一样的)

      可以这样读:

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

      其中appFeaturesplist 的名称。 (iOS 6.1)

      【讨论】:

        【解决方案3】:

        试试这个简单的方法:

        - (void)viewDidLoad
        {
            [super viewDidLoad];
            [self plistData];
        }
        
        
        - (void)plistData
        {
            NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
            NSArray *messages = [NSArray arrayWithContentsOfFile:file];
            NSLog(@"%d", messages.count);
        
            for (int i=0; i<messages.count; i++)
            {
                NSString *data = [messages objectAtIndex:i];
                NSLog(@"\n Data = %@",data);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多