[SOLVED]Having trouble reading an array of dictionaries from plist file.

Discussion in 'iOS Programming' started by chrono1081, Jul 28, 2014.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    Hi guys,

    I'm trying to figure out the best way to do the following:

    I have a list of Counties in a plist file, and each county has codes associated with it. It would look something like this:

    Code:
    
    Root
    
    ->Counties
    
    --->Elk County
    ----->Code 1
    ----->Code 2
    ----->Code 3
    
    --->Smith County
    ----->Code 1
    ----->Code 2
    ----->Code 3
    ----->Code 4
    ----->Code 5
    
    The problem is is I can't figure out how to store them in the plist so I can access the counties and the codes. I have tried many variations on the following (Including changing the main county to an array, the secondary counties to arrays, etc): 
    
    ->Root
    
    ->Counties (Dictionary)
    
    --->Elk County (Dictionary)
    ----->Code 1 (String)
    ----->Code 2 (String)
    ----->Code 3 (String)
    
    --->Smith County (Dictionary)
    ----->Code 1 (String)
    ----->Code 2 (String)
    ----->Code 3 (String)
    ----->Code 4 (String)
    ----->Code 5 (String)
    
    And I can't figure out how to get the values in the plist. I have other stuff that reads from plists just fine but those are just the Root, Arrays, and Strings under those arrays.

    The first puzzle is am I structuring my pList in such a away that I can grab the names of the counties (Elk, Smith) and read those into a picker, and then grab the values inside the counties (they each have a different number of strings). They also have to be under a Counties array (or dictionary if thats a better way) but it has to be under a counties entry (the county under root).

    Can anyone tell me if my pList is structured correctly to allow this?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Here's how I would structure the plist, based on my understanding of your data hierarchy:

    Code:
    ->Root
    
    -->Array (of counties)
    
    --->Item 1 (Dictionary)
    ---->"County Name" : "Elk County" (Key-Value Pair)
    ---->"Codes" : Array (Key-Value Pair)
    ----->"Code 1" (String)
    ----->"Code 2" (String)
    ----->"Code 3" (String)
    
    --->Item 2 (Dictionary)
    --->"County Name" : "Smith County" (Key-Value Pair)
    ---->"Codes" : Array (Key-Value Pair)
    ----->Code 1 (String)
    ----->Code 2 (String)
    ----->Code 3 (String)
    ----->Code 4 (String)
    ----->Code 5 (String)
    Let me know if you'd like to see the exact plist structure.
     
  3. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #3
    Thanks so much!

    The only part I'm confused about is the County Name: (Key Value Pair). Should this be a String? Thats what I put it as and it seems like I can access all of the values. I didn't see an option in the drop down for a key value pair and I wasn't sure if I should put it as a dictionary.

    Here's what I have, is this how you meant to do it?

    Code:
    <dict>
    	<key>Counties</key>
    	<array>
    		<dict>
    			<key>CountyName</key>
    			<string>Adams</string>
    			<key>Codes</key>
    			<array>
    				<string>201</string>
    				<string>202</string>
    				<string>203</string>
    			</array>
    		</dict>
    		<dict>
    			<key>CountyName</key>
    			<string>Elk</string>
    			<key>Codes</key>
    			<array>
    				<string>301</string>
    				<string>302</string>
    				<string>303</string>
    				<string>304</string>
    			</array>
    		</dict>
    	</array>
    </dict>
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Yeah, that's it.
     
  5. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #5
    Thanks!

    (I realized how stupid my second question was after I posted it but forgot to go back and change it).

    I haven't dealt with dictionaries and plists much in the past but figured they'd be a useful way to store data in this app and thought I'd use them.

    I'm also going to look into Core Data even though I know literally nothing about it. Apparently its faster but harder to use.
     

Share This Page