Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 19, 2009, 01:38 AM   #1
matthew858
macrumors member
 
Join Date: Apr 2008
Location: Sydney, Australia
Question About .plist Files

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?

Last edited by matthew858; Jan 19, 2009 at 02:00 AM.
matthew858 is offline   0 Reply With Quote
Old Jan 19, 2009, 10:54 AM   #2
xsmasher
macrumors regular
 
Join Date: Jul 2008
Quote:
Originally Posted by matthew858 View Post
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.
xsmasher is offline   0 Reply With Quote
Old Jan 19, 2009, 11:50 AM   #3
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
I'd suggest not storing this data in the .plist file but using NSUserDefaults (along with an NSDictionary, as suggested above) instead.
dejo is offline   0 Reply With Quote
Old Jan 20, 2009, 01:29 AM   #4
matthew858
Thread Starter
macrumors member
 
Join Date: Apr 2008
Location: Sydney, Australia
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.
matthew858 is offline   0 Reply With Quote
Old Jan 20, 2009, 09:07 AM   #5
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by matthew858 View Post
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.

Quote:
Originally Posted by matthew858 View Post
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.
dejo is offline   0 Reply With Quote
Old Jan 20, 2009, 09:15 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by dejo View Post
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).
robbieduncan is offline   0 Reply With Quote
Old Jan 20, 2009, 09:35 AM   #7
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by robbieduncan View Post
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.
dejo is offline   0 Reply With Quote
Old Jan 20, 2009, 09:40 AM   #8
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Mar 14, 2009, 12:15 PM   #9
shsgator81
macrumors newbie
 
Join Date: Mar 2009
Quote:
Originally Posted by PhoneyDeveloper View Post
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.
shsgator81 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to delete plist files? disasterdrone OS X Mavericks (10.9) 17 Jun 29, 2014 01:14 AM
iPhone: Editing plist files mareic Jailbreaks and iOS Hacks 1 Apr 16, 2014 04:45 AM
plist files in the trash zuma022 OS X Mavericks (10.9) 0 Nov 13, 2013 01:49 PM
Cannot Delete Plist files. MattPSD Mac Basics and Help 12 Sep 13, 2012 06:09 AM
PLIST Files esierr1 iPhone/iPad Programming 10 Aug 10, 2012 04:05 PM

Forum Jump

All times are GMT -5. The time now is 04:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC