
All development on the iPhone uses Objective-C on top of the Cocoa Touch framework. This project was my first experience using either Objective-C or Cocoa, having been mainly a Java guy for most of my life. After a good amount of time with it, I still have mixed feelings.
One of the most noticeable differences between Cocoa Touch and Java is memory management. Cocoa Touch does not feature automated garbage collection, but instead uses a reference counting scheme for memory management. New objects are created with alloc which allocates a new instance of a class or copy which copies an existing instance; both methods sets the retain count to one. Objects can then be retained, which increments the retain count and claims ownership of that object. When you are done with an object, it is sent the release message, which decrements the retain count. When an object’s retain count reaches zero, its dealloc method is called and then its memory freed. Objects can alternatively be added to an autorelease pool, which registers a release sometime in the future. This allows for temporary objects to be automatically released at the end of event cycles (such as a touch event).
Function calls in Objective-C are handled using a Smalltalk-style message passing system. Unlike Simula-style languages (such as Java or C++) where method names are bound, messages are resolved at runtime, leaving the receiving object the task of interpreting the message. This allows for easier dynamic typing and for the sending of messages to collections of objects and expecting only some to respond, without the fear of producing runtime errors. The weakness is the lack of type checking and the fact that the object to which the message is directed is not guaranteed to respond. Message passing also takes more time to execute because of the overhead of interpreting messages. I can see why this design is used, but personally prefer method calling over message passing.
One very noticeable difference that noticed in Objective-C is the lack of Namespaces. There are many arguments on both sides as to the necessity of having name spaces, but I personally find them an amazingly useful way to group and organize classes. A common technique to avoid name collisions is appending a domain prefix in front of class names (all Cocoa classes begin with NS and all Cocoa Touch classes begin with UI).
The Cocoa framework fully embraces the delegation pattern. This pattern works very well with the message passing nature of Objective-C and is very good at handling changes in behavior and requirements. It also does a good job of abstracting responsibilities. This combination allows protocols to require only partial implementation with optional methods for additional functionality.
* Please be aware that all comments are moderated.
Eileen | 5:19 PM February 11, 2010
For those app developers that don’t know Objective-C and Cocoa Touch and don’t want to outsource development, check out localbeacon (an iphone app builder) at http://www.bigforge.com. Great for those who want to build just one app or developers interested in white label. Full integration of Twitter and Facebook.