![]() | Improved States example from Chapter 5 |
|
16 Oct 2008, 20:18
Stefan Arentz (24 posts) |
I have created an improved version of the States example from chapter 5. There is one big thing that I did different here: The states database is now contained in a separate class, StatesDatabase. This class is instantiated from MainWindow.xib by using an NSObject nib item. It will load it’s database from a plist file and expose two simple getters to get the states sorted by either area or population. The StatesDatabase instance is then linked to both the StatesByAreaTableViewController and StatesByPopulationTableViewController simply by using the database outlet that I added to these controllers. This is a design with less dependencies and less manual glueing things together. I could remove a bunch of code from the StatesAppDelegate and the two View Controllers as a result. You can download my code from http://kip.sateh.com/States.zip |
|
19 Oct 2008, 04:23
Felipe Laso (104 posts) |
Thats awesome Stefan! I should try something like this out, I was having trouble following the chapter as it was very vague as to where the code in the book should be placed. But I made a new project and had no trouble getting the 2 view controllers to work :) I might try out your idea, its a good exercise and it’s also a much better implementation than cramming it all up in the StatesAppDelegate as the model/data source for everything. Just one question, how did you create the states data? Did you still use an NSDictionary and saved it into a plist file? Some insight would rock so that I can try out something like you did :) Cheers and many thanks in advance, |
|
21 Oct 2008, 20:21
Stefan Arentz (24 posts) |
I got the data from Wikipedia (did not know I could download the Book’s source code) and simply put it in a plist file as an array of dictonaries (records). Plist files are great to store static structured data since you can load them with a one-liner and then simply access them with NSDictionary, NSArray and friends. The iPhone supports Key-Value coding, which makes looking up values in nested dictionaries even simpler. See Key-Value Coding href=”http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/BasicPrinciples.html”>http://developer.apple.com/documentation/Cocoa/... SQLite is also great and might be more appropriate if you have a lot of data or if you want to do structured queries on that data. I personally find it a bit unfortunate that Apple did not include Core Data in the iPhone. Or at least a simple SQLite wrapper to make it more Cocoa friendly. There are probably some open source wrappers available though. S. |
|
24 Oct 2008, 04:59
Felipe Laso (104 posts) |
Thanks Stefan, I actually just read the chapter on key value coding from Aaron’s Cocoa Programming book, it is quite a handy way to look up values in NSDictionary, NSArray or other classes. I think it would be wise of me to spend more time learning how to implement a SQLite database, since it will allow me to save values and to provide a better model for my applications! But it was still a clever way to fix things, since I didn’t know how to implement a plist file, I made a class that was initialized with my main nib file, that way every viewController in my tab bar had the same instance variable with my states info. I’m sure you checked this out when you downloaded my project to help me :) Thanks again, |
| You must be logged in to comment |

