<?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">
<array>
<array>
<string>Title 1</string>
<string>Subtitle 1</string>
<string>image1.png</string>
</array>
<array>
<string>Title 2</string>
<string>Subtitle 2</string>
<string>image2.png</string>
</array>
</array>
</plist>
这是我正在处理的应用的Plist文件。
我不确定如何使用多个数组的第一个字符串来创建包含所有标题的NSMutableArray,再次为字幕和图像名称创建。
我目前的解决方法是为每种类型的字符串创建3个单独的Plist文件,这可能会造成麻烦。
答案 0 :(得分:2)
要在保留plist数据结构的同时回答您的问题,您可以执行以下操作:
NSArray *rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"your-plist" ofType:@"plist"]];
int numItems = [rootArray count];
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *subtitles = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:numItems];
for (NSArray *itemData in rootArray) {
[titles addObject:[itemData objectAtIndex:0]];
[subtitles addObject:[itemData objectAtIndex:1]];
[images addObject:[itemData objectAtIndex:2]];
}
但是,我建议您使用如下结构:
<?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">
<array>
<dict>
<key>title</key>
<string>Title 1</string>
<key>subtitle</key>
<string>Subtitle 1</string>
<key>image</key>
<string>image1.png</string>
</dict>
<dict>
<key>title</key>
<string>Title 2</string>
<key>subtitle</key>
<string>Subtitle 2</string>
<key>image</key>
<string>image2.png</string>
</dict>
</array>
</plist>
此时您可以使用更明确的代码,如下所示:
NSArray *rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"your-plist" ofType:@"plist"]];
int numItems = [rootArray count];
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *subtitles = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[titles addObject:[itemData objectForKey:@"title"]];
[subtitles addObject:[itemData objectForKey:@"subtitle"]];
[images addObject:[itemData objectForKey:@"image"]];
}