views:

78

answers:

2

I have a web service which returns certain "models" of which are all defined by a class in objective-c. Calls to RESTful methods will return either a singular model XML or a list of model XML elements.

<widget>
   <a>foo</a>
</widget>

or

<widgets>
 <widget>
    <a>foo</a>
 </widget>
  ....
 <widget>
    <a>foo</a>
 </widget>
</widgets>

I'm trying to come up with a way to organize my classes in such a way that parsing the object or list of objects is easy and ultimately adding more model objects is easy. It will obviously involve a delegate for each "model", but how do you organize it in such a way that makes it easy and elegant. A xml delegate for each model object? how to handle the lists? Perhaps some type of list delegate object which will then refer to the correct individual model delegate according to some list? (ie: widgets -> widget delegate)

I am going to have a single class which provides all the methods in the webservice.

- (Widget *)getWidgetById: (int) id;
- (some array) getWidgets:;

I guess its really more of a OO design pattern question then anything else.

+1  A: 

You might want to start by looking at the XMLPerformance Apple sample code. You may choose to use NSXMLParser or libxml, or just roll your own solution. Also see the XML parsing section of the Data Management Coding How-To's.

Jeff Kelley
Good suggestion. Lots of good code in the XMLPerformance app.
jr
+1  A: 

If I were you I'd strongly consider having the server return either JSON or PLISTs.

The advantage of those two formats is, that they are less open-ended than XML and so you naturally structure things into arrays, dictionaries, and variously typed elements which naturally map back into object properties and are very easy to parse (for JSON you need a library like TouchJSON, for plists you can use the built-in parsing). You also are not worrying about when to make something an element vs. an attribute.

Basically the formula is this: you get a response from the server, in one call turn the data into an NSDictionary or NSArray (usually an array of NSDictionaries) and then just pull out attributes from them into your data model.

Kendall Helmstetter Gelner
Thanks for the suggestion. Seems like alot less work then having the parser. I'll check this out. The nice thing is that I should be able to return both formats by just using the HTTP Accepts header. May be worth it.
jr