0


I've checked the other posts, but non seem to answer my question, so here I go :)
I've got a Plist, with the following contents:

<?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">
<dict>
    <key>001</key>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <key>002</key>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</dict>
</plist>

I'm trying to get this code to converted to an array and used for a TableView source. However, even thought I've seen other posts, none help me out (That I can see, this is my first true iOS Project, so I might not be 100% at manipulating their code). I want the table to be generated from this Plist, and since it's going to have at least 100 items (With more being added via the Plist), I'd like for it to be generated.
However, the main thing I'm struggling to do is get the array to get the information from Key1, then Key2, Key3... like it would in the following manual code (Which I have to type out manually, and I'll be adding more info, so I want it to come from the Plist):

NSDictionary *item1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                          @"Name1", @"Name", @"001", @"Number", nil]; 
NSDictionary *item2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name2", @"Name", @"002", @"Number", nil];
NSDictionary *item3 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name3", @"Name", @"003", @"Number", nil];

One thing I would like is to be able to change the order the table is shown in (Between number and alphabetic), which I don't think is hugely relevant, but it's something I'd like to throw in there, incase I'm making a mistake with something meaning that can't be done?
Thank you for reading,
Joseph Duffy

Edit: Solved, but new issue, please read below. Now solved, thank you! Thank you for the information, it has been most useful! However, when I put the following:

- (void)viewDidLoad
{
    NSBundle *bundleLocation = [NSBundle mainBundle];
    NSString *arrayLocation = [bundleLocation pathForResource:@"plistname" ofType:@"plist"];
    NSArray *plistcontentsArray = [[NSArray alloc] initWithContentsOfFile:arrayLocation];
    self.pokemonNames = pokemonArray;

    [bundleLocation release];
    [plistArray release];

    [super viewDidLoad];
}

Everything works fine. But when I add [arrayLocation release]; it crashes with "exc_bad_access". Any ideas? :)

Joseph Duffy
  • 4,566
  • 9
  • 38
  • 68
  • NSDictionary and NSArray both have a `initWithContentsOfFile` method that can be used to get a dict/array from a plist – albertamg May 02 '11 at 14:56

1 Answers1

1

Simply use the "array" element instead of the "dict" element in your property list. For example:

<?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>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</array>
</plist>

Also are you just going to store names or are you going to store more information in the nested dictionaries? If not then this can be reduced down to:

<?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>
    <string>Name1</string>
    <string>Name2</string>
</array>
</plist>

To load the array in the property list simply use the following. (Changing "Array" to the name of the property list and more than likely using an instance variable for the NSArray.)

NSBundle * mainBundle = [NSBundle mainBundle];
NSString * arrayLocation = [mainBundle pathForResource:@"Array" ofType:@"plist"];
NSArray * array = [[NSArray alloc] initWithContentsOfFile:arrayLocation];

and to pull the information out of the array you can use:

NSString * name = [array objectAtIndex:0];

As for doing alphabetical order you can find how to do that here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5

  • You should only release something you take ownership of, ie you call alloc,copy,new or you retain it. Since you don't do that, you don't need to release it. – Jamie May 02 '11 at 16:30
  • Thanks for the help! x2 I've still got some things to learn, so thank you for your time :) – Joseph Duffy May 02 '11 at 16:31