loading and access plist data

Discussion in 'iOS Programming' started by StefanDevil, Aug 15, 2010.

  1. StefanDevil macrumors member

    Jul 31, 2010

    i have a plist in the following form:

    Root (array)--->

    item 1 (dictionary)
    ----> Sch (string)
    ---> Name (string)
    ----> price (number)

    item 2 (dictionary)----> .....same as item 1

    how can i access each row (item1 to ...) and the its child (Sch, Name etc.)? one at a time?

    i use:

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:mad:"Data.plist"];
    NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
    to load the file. how such i go about accessing each child?

    what i am trying to do is, i have a NSString *messeage, what i want to do is to search the whole plist for matching string and display the whole item 1. Any suggestion?

    Thanks for the help! gd day!
  2. nebo1ss macrumors 68030

    Jun 2, 2010
    Not sure what computer you have but on a MAC I use PListedit pro
  3. StefanDevil thread starter macrumors member

    Jul 31, 2010
    i mean how can i access, as in load the plist, in xcode and retrieve each data.
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You show the plist as being an array of dictionaries but the code you show loads the plist as a dictionary. Assuming it's really an array then load the plist into an array. Then you iterate the array just like any other array to access its members.
  5. StefanDevil thread starter macrumors member

    Jul 31, 2010
    thks for the help, i manage to solve this problem.
    now i have another problem.
    i have a new plist in this form:

    i load the plist using:

    NSString *path = [[NSBundle mainBundle] pathForResource:
                        @"data" ofType:@"plist"];
        // Build the array from the plist  
        NSMutableArray *array3 = [[NSMutableArray alloc] initWithContentsOfFile:path]; 
    what i want to do is, i have a "label.text" which is some numbers,
    i would like to go to the match key (11231654 for example) and search its child.

    so firstly, i must compare label.text with all the key available.

    i 1st try to load one key and display it(just to make sure i am accessing the data correctly)

    i use:

    		NSString *myString = [array3 objectAtIndex:0];	
    		label.text = myString;
    this will make label.text =11231654 ?

    but its not displaying?
  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    You've made the same error as your original post, but in reverse.

    Your original code was wrong because you were loading an <array> plist into an NSDictionary.
    Your latest code is wrong because you're loading a <dict> plist into an NSMutableArray.

    These data structures are not interchangeable.

    You must load a <dict> plist file into an NSDictionary (or its subclass NSMutableDictionary). Similarly, an <array> plist file must be loaded into an NSArray (or its subclass NSMutableArray).
  7. StefanDevil thread starter macrumors member

    Jul 31, 2010
    i see, i have change it. now how can i get the key value, 11231654?

    as in i would to have a NSString = *number;
    number will be equal to 11231654, so that i can use number to compare will others.

    thks for the help!
  8. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Think it through. There's nothing difficult here.

    How do you usually retrieve the object for a key from an NSDictionary?
    With objectForKey:.

    Since plist keys are always strings (Ref. 1),you can use any expression that results in a numeric NSString object. Examples:
    [NSString stringWithFormat:@"%i", 11231654]
    [[NSNumber numberWithInteger:11231654] stringValue]

    (Ref. 1)
    When using CFDictionary as a property list, all keys must be strings.
  9. StefanDevil thread starter macrumors member

    Jul 31, 2010
    hi, thks for the reply.

    if i use:
    array3 objectForKey:@"11231654"
    i will be "looking" at the array inside the key 11231654, correct?

    how can i "look at" the key(s) inside array3?

    correct me if, i am wrong, but i should not be using objectforkey here.
  10. grimjim macrumors member

    May 24, 2003
    What "key(s) inside array3"? It's an array. It doesn't have keys. What you have done is to return the NSArray object whose key is "11231654" that is stored in the plist. If you want to get at the contents of the array, you do so in the same way as any other NSArray. If you look at the contents of the plist that you posted, you will see that the key "11231654" contains two string objects. No keys are specified for them.

    So, yes, you should use objectForKey: to return the NSArray object, and then fast enumeration or a simple for loop to iterate through it and work on the objects it contains. Of course, you will need to be sure that array3 is of the correct type (i.e. that it is an NSArray, not an NSDictionary).

Share This Page