NSExplorer
NSExplorer

Reputation: 12149

Sample code illustrating MVC architecture in iOS

I'm trying to get the hang of MVC architecture. Say I have a plist in which there are a list of persons and each person has a few attributes like name, address and photograph. Suppose I want to display these details in a table view. The cell title would be the name, description would be the address and the image towards the left would be the photograph of the person.

One approach I could take is to load the plist in an array-of-dictionaries in my viewDidLoad: and then display them.

However, I want to adopt an Object Oriented method by creating 'Person' class. How do I go about doing the same in this case? I believe I could start by creating a 'Person' class with three attributes:Name, Address, Photograph. What next? I would need many instances of this 'Person' class right? How would I 'load' each instance with a corresponding Person entry from the plist? Should I create another class that does this 'loading'? Do people use Singleton class to achieve his?

Could someone share some sample sample code to illustrate this? Or maybe guide me to blogs/resources that talk about this?

Upvotes: 4

Views: 2470

Answers (2)

Tatvamasi
Tatvamasi

Reputation: 2547

I believe you are not looking for a design solution to the above mentioned question. If that is the case @A Salcedo 's version looks fine.

if you are looking for general guide lines for MVC and modeling , Martin Fowler's site offers some of the best (agile) design/modeling guidelines.
http://www.martinfowler.com/eaaDev/uiArchs.html (on MVC) and
http://martinfowler.com/design.html (many interesting design related posts).

Happy reading.

Upvotes: 1

A Salcedo
A Salcedo

Reputation: 6478

Hmmm, I think you are over thinking this a bit. I would just create a class that would handle my person, in this case your 'Person' class.

I would simply store each person using Core Data. Then, when it's time to display them, I would just make a fetch request and store all person managed objects into an NSMutableArray (which simply handles arrays of objects). Then you can simply use the index value to display the numerous persons in your array in a tableView.

In summary I would:

1. For every person, create instance of Person.
2. Verify if person exists in my Core Data Person Entity.
3. If not, then insert into Core Data (the object will become an
NSManagedObject).
4. For displaying, simply do a fetch request to pull all persons in your
entity. Here I prefer to store the
results into an NSMutableArray, but
that is completely up to you. Make
sure you release your fetch request
after the results are store in the
array.
5. Reference them to your table view using the index value for each
person NSManagedObject in the array.

For something that doesn't involve storing simply:

1. Create instance of Person for every entry.
2. Add Person object to array.
3. Reference each Person to table view using index value.

In the end the approach that you take will be dictated by what you want to do with the information.

As for reading the plist, I would opt for reading an XML for which all you need is an XML Parser class (there are several options for parsers). Since I don't do anything but parse the XML, I use NSXMLParser, but that choice is also up to you. Just create an NSXMLParser class (make sure that the different actions for when the parser finds a given element are in play inside that parser). So yes, you would need to make additions to the NSXMLParser for handling each element. It's really easier than it sounds.

Also, by storing in Core Data, you can always fetch the info without you using a Singleton.

Upvotes: 4

Related Questions