Plist中的多个阵列 [英] Multiple Arrays From Plist

查看:142
本文介绍了Plist中的多个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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文件。

This is my Plist file for an app I am working on.

我不知道如何使用多个数组的第一个字符串创建一个包含所有标题的NSMutableArray,再次为字幕和图像名称。

I am not sure on how to take the first string of many arrays to create an NSMutableArray with all the titles and again for the subtitles and image names.

我目前的解决方法是为每种类型的字符串创建3个单独的Plist文件,这可能会令人讨厌。

My workaround at the moment is creating 3 separate Plist files for each type of string which can be a nuisance.

推荐答案

要在保留plist数据结构的同时回答您的问题,您可以执行以下操作:

To answer your question while retaining the plist data structure, you could simply do something like the following:

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]];
}

但是,我建议您使用以下结构:

However, I would advise you to use a structure like:

<?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>

此时您可以使用更明确的代码,如下所示:

At which point you could use the more explicit code as follows:

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"]];
}

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

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