PDA

View Full Version : Question About .plist Files




matthew858
Jan 19, 2009, 01:38 AM
What is the easiest way to grab only some info from a .plist file and display it in a table view?

Example:
I have a .plist that has different shops stored in it, with each of the shop's states within the .plist file in a string called "States". Each store has an individual item in the .plist file. I want to have different tab bars displaying different states, with the shops from just that state displayed in the table view.

What is the best way to programmatically manipulate what is displayed from a .plist file in a table view?



xsmasher
Jan 19, 2009, 10:54 AM
What is the easiest way to grab only some info from a .plist file and display it in a table view?

Example:
I have a .plist that has different shops stored in it, with each of the shop's states within the .plist file in a string called "States". Each store has an individual item in the .plist file. I want to have different tab bars displaying different states, with the shops from just that state displayed in the table view.

What is the best way to programmatically manipulate what is displayed from a .plist file in a table view?

NSDictionary has a method "dictionaryWithContentsOfFile:" that will load your entire plist at once, if its root is a dictionary. Otherwise you'll have to us NSXMLParser.

Then you want to go from "list of shops, with states for each" to "list of states, with shops for each," right? You'll have to do something like this:

(1) get list of keys from the store-to-state dictionary
(2) loop through the keys; for each key:
(2b) split the string of states into an array
(2c) loop through the state array; for each state:
(2c.1) add the state-to-store pair to another dictionary

Now you have a dictionary you can use in the data source for the table.

dejo
Jan 19, 2009, 11:50 AM
I'd suggest not storing this data in the .plist file but using NSUserDefaults (along with an NSDictionary, as suggested above) instead.

matthew858
Jan 20, 2009, 01:29 AM
Unfortunately putting the data into NSUserDefaults is not an option, as I already have data in a .plist file that contains data. Or do you mean to take the data out of a .plist file and store it into NSUserDeafaults?

Whichever way you suggest, do you have some sample code or documentation on the various parts I need to do? I am new to iPhone app development and would like to learn how to do this fairly quickly.

dejo
Jan 20, 2009, 09:07 AM
Or do you mean to take the data out of a .plist file and store it into NSUserDeafaults?
Yes, that is what I'm suggesting.

Whichever way you suggest, do you have some sample code or documentation on the various parts I need to do? I am new to iPhone app development and would like to learn how to do this fairly quickly.
Looking up NSUserDefaults in the Developer Documentation, shows me that there are five apps of Related Sample Code: AppPrefs, BubbleLevel, DrillDownSave, Metronome, and MoviePlayer.

Also, although I understand the temptation to want to get results quickly, I'd suggest concentrating on learning how to do it more thoroughly.

robbieduncan
Jan 20, 2009, 09:15 AM
I'd suggest not storing this data in the .plist file but using NSUserDefaults (along with an NSDictionary, as suggested above) instead.

I'd fundamentally disagree. NSUserDefaults is intended for storing user preferences that get updated. It is not stored within the app bundle, rather in a writable location. If this is static data (which it sounds like it is) then a .plist (or other) file in the resources of the app is the correct location.

Loading it should be simple using the methods in NSDictionary or NSArray (depending on the root element).

dejo
Jan 20, 2009, 09:35 AM
If this is static data (which it sounds like it is) then a .plist (or other) file in the resources of the app is the correct location.
Ah, agreed. Didn't pick up on the static-ness of the data. My apologies to the OP.

PhoneyDeveloper
Jan 20, 2009, 09:40 AM
Whether the data are in a dictionary in a plist file or a dictionary in NSUserDefaults really isn't important.

Work from your table backwards. What data structure do you need to populate your tableview? Write code that reads your plist and builds that data structure.

I usually use an array of arrays of dictionaries for a sectioned tableview. The dictionary represents a row, the array that holds the dictionaries represents a section, and the top level array holds all the sections. In your case the section array would represent the states.

shsgator81
Mar 14, 2009, 12:15 PM
Whether the data are in a dictionary in a plist file or a dictionary in NSUserDefaults really isn't important.

Work from your table backwards. What data structure do you need to populate your tableview? Write code that reads your plist and builds that data structure.

I usually use an array of arrays of dictionaries for a sectioned tableview. The dictionary represents a row, the array that holds the dictionaries represents a section, and the top level array holds all the sections. In your case the section array would represent the states.

Could anyone point me to a good example (or several) of using NSDictionary that retrieves a multi-level set of keys/values from a plist? Before going the SQLite route, I would like to test traversing my data using the array of arrays approach suggested here.

Also, what is the best approach to "retaining" this data across several view controllers? As you can no doubt tell, I am an iPhone dev newbie.

Many thanks in advance for your help.