Object-Oriented Design in iPhone and iPad Programming

Something I enjoy about teaching for Learning Tree is that, despite using basically the same materials for a given course, every class ends up being slightly different. That’s down to the people that come on the course, the experience they bring to the class and how they interact as a group. The best courses end up being extended discussions between individuals with shared objectives, rather than a one-way lecture from instructor to students.

I had a really good group of students attending my most recent run of Learning Tree’s Building iPhone® and iPad® Applications: Extended Features course. They had lots of enthusiasm for the course and generated plenty of interesting discussion.

One of the things we discussed a lot on that course was making use of more advanced Objective-C features to write better code. Many people come to Learning Tree’s introductory iPhone® and iPad® programming course with no Objective-C knowledge at all and we cover just enough of the language on that course to start building applications. By the time students have had a chance to practice what they learned and are ready to follow-up with the extended features course, they tend to have more of an appetite for deeper Objective-C knowledge.

The motivation to gain deeper Objective-C knowledge often comes after someone has developed a couple of applications and noticed that they are repeating the same changes to template code or copying and pasting code between projects. Even programmers with years of object-oriented design experience seem to do this when they start iPhone and iPad programming–I know I did.

I’ve done lots of object-oriented programming but, despite my Java projects being neatly organised into tiers, making extensive use of inheritance, encapsulation, polymorphism, programming to interfaces, dependency injection, design patterns and the rest, I found my early iPhone projects descended into an alarming reliance on copy and paste!

Looking back, I think the reason for this is the way Xcode, the development tool for iPhone and iPad programming, makes extensive use of templates. It certainly saves typing and makes creating your first applications simpler, but once you have a couple of applications under your belt, I think it’s important to consider whether those templates are encouraging you to create well structured applications.

Creating an application using Core Data, Apple’s object-relational-mapping framework, is a good example. Xcode has a checkbox that you can check on project creation to add Core Data support to a project. The problem is that when you start modifying that code, as I have been doing recently to get Core Data data stores replicating across iCloud, the next time you create a project using the template, your changes are lost. Far better to do what we do on the extended features course and move that code into a re-usable class that you can make use of in similar projects.

Pushing code up into parent view controllers is another obvious object-oriented programming technique that can improve code re-use, but it is something that you need to make a conscious effort to do when using Xcode templates. Once you do though, you can start deleting lots of generated code from your projects and that’s a sure sign that you are improving code re-use!

Once you have created re-usable classes of course, the ideal way to share them across projects is to create code libraries–your own frameworks that you can fine-tune and use again and again in your projects. Learning Tree’s Hands On Objective-C Programming Fundamentals for the Mac/iPhone®/iPad® covers the creation of libraries and also has plenty of coverage of good object-oriented design principles.

I think it’s important for us all as iPhone and iPad programmers to take stock periodically, to think about what we are doing in terms of code re-use and maintainability. It’s not difficult to apply the same principles that we apply to development in other languages, but we do need to make an effort to adjust those tried and tested principles to new tools and ways of working.

Richard Senior

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.