多张阵列从plist中 [英] Mulitple Arrays From Plist

查看:276
本文介绍了多张阵列从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.

任何建议将是极大的AP preciated。

Any suggestions would be greatly appreciated.

推荐答案

要回答你的问题,同时保留的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>

在这一点上,你可以使用更明确的code如下:

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天全站免登陆