开发者

Reading information from a Plist to use in TableView

开发者 https://www.devze.com 2023-03-02 10:43 出处:网络
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:

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:@"plistna开发者_C百科me" 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? :)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号