Plist Array到NSArray没有对象 [英] Plist Array to NSArray no objects

查看:111
本文介绍了Plist Array到NSArray没有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经搜索过,并且之前已经成功完成了这项工作,但是我遇到了这个问题。

So I've searched, and read and have done this successfully before, but am running into a problem with this one.

我有一个plist是一个字符串数组。我没有被读入数组的对象。我成功地为另一个控制器中的字典做了这个。所以这是我的代码。

I have a plist that is an array of strings. I get no objects being read into the array. I successfully do this for a dictionary in another controller. So here is my code.

    // Open plist and get brands
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"brands" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:plistPath]) {
    NSLog(@"The file exists");
} else {
    NSLog(@"The file does not exist");
}

_brands = [NSArray arrayWithContentsOfFile:plistPath];

_catList = [[[NSMutableArray alloc] initWithCapacity:[_brands count]] autorelease];

这是我的plist。请注意,_brands数组被定义为NSArray *品牌,属性设置为非原子,readonly并合成为brands = _brands。

and here is my plist. Note that the _brands array is defined as NSArray *brands, property set as nonatomic, readonly and is synthesized as brands = _brands.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Root</key>
    <array>
        <string>Brand A</string>
        <string>Brand B</string>
        <string>Brand C</string>
        <string>Brand D</string>
    </array>
</dict>
</plist>

我看到有一个标签,但在Xcode中它显示为带字符串的数组。不是字典>数组>许多字符串

I see that there is a tag, but in Xcode it shows as an array with strings. Not a dictionary>array>many strings

推荐答案

字符集本质上是字典(请注意< dict>标记)。将文件读入NSDictionary,然后使用 objectforKey:方法和 Root 键向字典询问数组。 / p>

PLists are dictionaries at their heart (notice the <dict> tag). Read the file into an NSDictionary, then ask the dictionary for the array using objectforKey: method and the Root key.

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
_brands = [dict objectForKey:@"Root"];

这篇关于Plist Array到NSArray没有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆