I’ve completed the first two online tutorials “Your First iOS App” and “Your Second iOS App”. The latter makes use of a data controller class as seen here.
I’m now making my way through “iOS Programming: The Big Nerd Ranch Guide 3rd Edition”. It seemingly makes no reference to data controller classes, however, and seems to be putting methods into the model class itself (i.e., into MyClass.m, not MyClassDataController.m).
I’m sure all will become clear eventually, but can someone give me a wider frame of reference? Is the use of a data controller class just a stylistic decision? I’ve looked in a couple of other books and they don’t seem to refer to data controllers, either.
That’s a great question! To be honest, both approaches are acceptable. Aaron Hillegrass (who co-authors the BNR books) is a very well respected developer (who even used to train Apple engineers), and obviously the Apple sample code you looked at is ‘from the horse’s mouth’, so to speak.
Why does Apple suggest you use a separate data controller in their app? Well, if your data controller conforms to the relevant protocols you could plug it straight into something like a
UITableView. If you were writing a universal iPad / iPhone app that had different views for the two devices then this could be quite useful. This is also quite useful if you’re working with storyboards. As Apple say:But this can be overkill: there are plenty of apps that don’t use this pattern, and they throw that logic into the view controller itself. There are pros and cons to both. To put it another way: as you navigate around the sample code Apple put on their developer website you’ll find plenty of apps that don’t follow the pattern Apple suggest in the ‘Your Second iOS App’ tutorial!