I'm using Foursquare to get a List of Restaurants nearby inside an iOS app. The Result is stored in an Array which consists of Dictionaries and regarding how deep the Data is, each Dictionary contains also an Array with Dictionaries and so on. The Result should be shown inside a UITableView. Should I create a Class that represents a Restaurant and then create an instance for every Restaurant which I put in an Array and use this as the Data Source for my UITableView or should I use the complete Array I got the first Time. I think, using an Array with a dedicated Restaurant class would make the handling much easier but maybe there are reasons to not do it this way? Performance maybe?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
I would go with specific classes - it will be much easier to write unit tests and maintain code even if you lose some performance. It is not a place where you should prefer performance over clarity.
I have not been in iOS programming for a long time, so double check my answer.
My rule of thumb: create a class when you want to encapsulate a certain functionality. If you want an "intelligent" Restaurant object (some business logic, validation, calculated fields), you should create that class. To follow Apple conventions, you should then implement NSCoding protocol that transfers the class fields to and from Dictionaries, and use a JSON serializer that can transform the standard objects in the JSON file to Restaurant instances using your code. This sounds a "strong" solution, but has its flaws.
iOS is screen-oriented (business logic in the view controllers), and you would spend a lot of time writing that NSCoding functions only to use Restaurants instead of Dictionaries behind the UITableViewDataSource callback functions (which you will have to write the same way in either case).
So in a simple "proof of concept" case, I would recommend not playing a lot with NSCoding, Restaurant and other stuff, but using the NSArray of Dictionaries coming from the JSON. Primarily not for runtime performance, but the amount of programming time, code complexity - and the possible errors. You can switch to using dedicated classes if you really have to: if using dictionaries would make your code uncomfortable.