[英]Plist Array to NSArray no objects
So I've searched, and read and have done this successfully before, but am running into a problem with this one. 因此,我之前已经搜索,阅读并成功完成了此操作,但是此操作遇到了问题。
I have a plist that is an array of strings. 我有一个plist,它是一个字符串数组。 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];
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.
请注意,_brands数组定义为NSArray * brands,属性设置为非原子,只读,并合成为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>
I see that there is a tag, but in Xcode it shows as an array with strings. 我看到有一个标签,但是在Xcode中,它显示为带有字符串的数组。 Not a dictionary>array>many strings
不是字典>数组>许多字符串
PLists are dictionaries at their heart (notice the <dict> tag). PLists是他们内心的词典(请注意<dict>标签)。 Read the file into an NSDictionary, then ask the dictionary for the array using
objectforKey:
method and the Root
key. 将文件读入NSDictionary,然后使用
objectforKey:
方法和Root
键向字典查询数组。
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
_brands = [dict objectForKey:@"Root"];
NSString *plistPath = [self copyFileToDocumentDirectory:plistFileName];
NSDictionary *plistContents=[[NSDictionary alloc] initWithContentsOfFile:plistPath];
[plistPath release];
return plistContents;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.