Friday, June 12, 2009

Lessons from the HelloWorld application

Creating the HelloWorld application opened my eyes to several things. Firstly, Objective-C and Cocoa are unlike any other programming language I've ever learned. The syntax is not very straight-forward. That means I'll probably have to spend a few weeks at the very least to learn the basics of Objective-C and Cocoa programming. Secondly, the concept of memory management for the iPhone is something that requires a lot of attention (i.e - is a bitch for the programmer).

"In general, however, you should try to avoid using autorelease wherever possible as it’s a more resource intensive operation than release."

Apple recommends that iPhone programmers follow these memory management guidelines:

To make sure it is clear when you own an object and when you do not, and what responsibilities you have as an owner, Cocoa sets the following policy:

  • You own any object you create.

    You “create” an object using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy).

  • If you own an object, you are responsible for relinquishing ownership when you have finished with it.

    You relinquish ownership of an object by sending it a release message or an autorelease message (autorelease is discussed in more detail in “Delayed Release”). In Cocoa terminology, relinquishing ownership of an object is typically referred to as “releasing” an object.

  • If you do not own an object, you must not release it.

This policy applies both to GUI-based Cocoa applications and to command-line Foundation tools.

The entire documentation can be found here:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html#//apple_ref/doc/uid/10000011

No comments:

Post a Comment