Iphone Plist数组到NSArray无对象
所以我以前已经搜索过,阅读过,并且成功地完成了这项工作,但是我遇到了一个问题 我有一个plist,它是一个字符串数组。没有对象被读入数组。我成功地为另一个控制器中的字典执行了此操作。这是我的代码Iphone Plist数组到NSArray无对象,iphone,objective-c,ios,ipad,plist,Iphone,Objective C,Ios,Ipad,Plist,所以我以前已经搜索过,阅读过,并且成功地完成了这项工作,但是我遇到了一个问题 我有一个plist,它是一个字符串数组。没有对象被读入数组。我成功地为另一个控制器中的字典执行了此操作。这是我的代码 // Open plist and get brands NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"brands" ofType:@"plist"]; NSFileManager *fileManager = [
// 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];
这是我的护照。请注意,_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>
我看到有一个标记,但在Xcode中它显示为带有字符串的数组。不是字典>数组>许多字符串PLists的核心是字典请注意标记。将文件读入NSDictionary,然后使用objectforKey:方法和根键向字典请求数组
plist的核心是词典,请注意标签。将文件读入NSDictionary,然后使用objectforKey:方法和根键向字典请求数组
谢谢我本来打算把它设置成一本字典,但我一直在寻找将plist直接读入数组的老教程和示例。在较旧的iOS版本中允许这样做吗?我不知道是否允许这样做。我总是把它们读入字典。我遇到过很多这样的例子:谢谢!我本来打算把它设置成一本字典,但我一直在寻找将plist直接读入数组的老教程和示例。在较旧的iOS版本中允许这样做吗?我不知道是否允许这样做。我总是把它们读入字典。我遇到过很多这样的例子:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
_brands = [dict objectForKey:@"Root"];
NSString *plistPath = [self copyFileToDocumentDirectory:plistFileName];
NSDictionary *plistContents=[[NSDictionary alloc] initWithContentsOfFile:plistPath];
[plistPath release];
return plistContents;