Alloc individual dictionary values to NSString

Discussion in 'iOS Programming' started by isaaclimdc, Jul 29, 2009.

  1. isaaclimdc macrumors 6502

    isaaclimdc

    Joined:
    Jun 28, 2009
    Location:
    Pittsburgh, PA
    #1
    Can anyone tell me how do I extract and alloc individual values in my plist array of dictionaries to some NSString?

    For example, I key in names and their emails:

    Code:
    <array>
    	<dict>
    		<key>Name</key>
    		<string>John Doe</string>
    		<key>Email</key>
    		<string>jdoe@gmail.com</string>
    	</dict>
    	<dict>
    		<key>Name</key>
    		<string>Alex Smith</string>
    		<key>Email</key>
    		<string>asmith@gmail.com</string>
    	</dict>
    </array>
    How do I display Alex Smith's email in a text label in some field?
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #2
    Have you tried loading the plist into an NSDictionary and using objectForKey?
     
  3. isaaclimdc thread starter macrumors 6502

    isaaclimdc

    Joined:
    Jun 28, 2009
    Location:
    Pittsburgh, PA
    #3
    Yes I did, but how do i load an array of dictionaries into a single dictionary? i'm pretty confused..
     
  4. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #4
    You have to build a new dictionary of dictionaries after deciding on a unique key in your data.

    If there is no unique key, stick with the array.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Why do you need to? Why not load it into an array of dictionaries? Then you can access each of those dictionaries via the array's index.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Code:
    NSArray* plist = [NSArray arrayWithContentsOfFile:pathToPlistFileInMyBundle];
    
    for (NSDictionary*d in plist)
    {
         NSString* name = [d objectForKey:@"Name"];
         NSString* email = [d objectForKey:@"Email"];
         // do something with the strings here
    }
     
  7. isaaclimdc thread starter macrumors 6502

    isaaclimdc

    Joined:
    Jun 28, 2009
    Location:
    Pittsburgh, PA
    #7
    Cool... your suggestions sound great. Will test them out pronto.
    Thanks!
     

Share This Page