loading and access plist data

Discussion in 'iPhone/iPad Programming' started by StefanDevil, Aug 15, 2010.

  1. macrumors member

    Joined:
    Jul 31, 2010
    #1
    Hi,

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

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

    Joined:
    Jul 31, 2010
    #3
    i mean how can i access, as in load the plist, in xcode and retrieve each data.
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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. thread starter macrumors member

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

    Code:
    <dict>  
       <key>11231654</key>
      <array>
          <string>hello</string>
          <string>goodbye</string>
      </array>
      <key>78978976</key>
      <array>
            <string>ok</string>
        <string>cancel</string>
       </array>
    
    i load the plist using:

    Code:
    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:

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

    but its not displaying?
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    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. thread starter macrumors member

    Joined:
    Jul 31, 2010
    #7
    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. macrumors 603

    Joined:
    Aug 9, 2009
    #8
    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:
    Code:
    @"11231654"
    
    [NSString stringWithFormat:@"%i", 11231654]
    
    [[NSNumber numberWithInteger:11231654] stringValue]
    
    someLabelContainingNumbers.text
    

    (Ref. 1)
    http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man5/plist.5.html
    When using CFDictionary as a property list, all keys must be strings.
     
  9. thread starter macrumors member

    Joined:
    Jul 31, 2010
    #9
    hi, thks for the reply.

    if i use:
    Code:
    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. macrumors member

    Joined:
    May 24, 2003
    #10
    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