Even if you're going to use a technology to avoid dealing with manual memory management, it's critical to learn it anyway. It's such a fundamental and important part of Cocoa development, not only because of how important it is to making efficient apps, and to not crashing, but also because you need to understand concepts and conventions of ownership in Cocoa. You need to know what you're doing when you're casting CoreFoundation and Foundation types. You need to know what a pointer is, know what a dangling pointer is, know how you can pass a pointer to a pointer to get a second return value from a method (`NSError`[1] is a pretty common Cocoa convention). Know what a singleton is.
And after you've learned all of this, you can also do some really neat things with Cocoa's dynamic runtime. Things like invocation building using a category and code like this (or similarly, store and queue up actions on the Mac's Undo manager):
Once you understand memory management, you can start using blocks, and even storing blocks. You need to understand how copying a block retains every object in the stack, and why raw type variables become immutable in a block. Then you can start to really have fun with awesome powerful technologies like Grand Central Dispatch.
Also, in Objective-C you should never user the Garbage Collection feature. And Automatic Reference Counting, while awesome, is not production-ready yet. Apple will not approve apps with it yet—I know, I've been rejected for trying to use it in an app.
[1]: HN's markdown parsing is a little funky, this is meant to say:
"Also, in Objective-C you should never user the Garbage Collection feature."
Why not? On the Mac, the garbage collection process is offloaded to another CPU. Any performance hit is more than made up for the fact that developers probably messed up the retain counts.
Memory management is a thing that you have to get perfect or your app will crash. I personally trust ARC or GC more than I trust myself.
iOS's Objective-C doesn't support garbage collection. iOS 5 will add support for Automatic Reference Counting, but no real garbage collection. So if you are writing cross-platform Objective-C code that must support iOS devices and Macs, then you can't rely on garbage collection. And if you must support iOS < 5, then you can't rely on Automatic Reference Counting either.
It's foreseeable that GC will go the way of the dodo in one of the next OS X releases. (They didn't mention GC once at the WWDC talks. Not even in the memory management related talks.)
> I personally trust ARC or GC more than I trust myself.
I don't. Reference counted memory management is so easy that I never had the problem of leaking/crashing apps because of forgotten retain/releases. The most common memory management related bug for me are hidden retain cycles. (Which ARC doesn't really address.)
But ARC is pretty nice for the stupid UI boilerplate code which saves you from writing huge -dealloc methods. For critical code (Obj-C and C intermixing) I tend to disable ARC.
> Apple will not approve apps with it yet—I know, I've been rejected for trying to use it in an app.
Just change the Info.plist of your app (so that it looks like it was built with the non beta of XCode), re-codesign the bundle and upload it. (Have tested it with OS X apps. No guarantee for iOS.)
And after you've learned all of this, you can also do some really neat things with Cocoa's dynamic runtime. Things like invocation building using a category and code like this (or similarly, store and queue up actions on the Mac's Undo manager):
Once you understand memory management, you can start using blocks, and even storing blocks. You need to understand how copying a block retains every object in the stack, and why raw type variables become immutable in a block. Then you can start to really have fun with awesome powerful technologies like Grand Central Dispatch.Also, in Objective-C you should never user the Garbage Collection feature. And Automatic Reference Counting, while awesome, is not production-ready yet. Apple will not approve apps with it yet—I know, I've been rejected for trying to use it in an app.
[1]: HN's markdown parsing is a little funky, this is meant to say: