Plist 中的多个数组

发布于 2024-11-13 05:41:28 字数 729 浏览 3 评论 0原文

<?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 文件,这可能会很麻烦。

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-11-20 05:41:28

要在保留 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"]];
}

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"]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文