Multiple Arrays From Plist
Asked Answered
E

1

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

Enzyme answered 31/5, 2011 at 19:18 Comment(0)
F
2

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"]];
}
Frankpledge answered 31/5, 2011 at 20:50 Comment(2)
Great, i'll give that a go. Another app i'm working on will deal with many more items so i will need this.Enzyme
I have run into a slight problem. I am trying to get the image when a row from my uitableview is clicked. titles and subtitles work and technically images works too, but images only work in cellForRowAtIndexPath. I need it to work on didSelectRowAtIndexPath, however, the app aborts and gives the error NSCFString objectAtIndex unrecognized selector sentEnzyme

© 2022 - 2024 — McMap. All rights reserved.