Posts Tagged ‘Cocoa’

iPhone SDK Cocoa Objective-C Learnings

I’ve been pretty heavily immersed in Apple Objective-C world lately and wanted to put down some of the things that I’ve learned. I’m going to post a little something when ever I find a few moments.

Here’s the first:

  1. It’s easier when someone else worries about memory management.

That being said, it’s not too hard to get used to managing it yourself. The main rule to keep in mind is this.

If you Alloc/Init the object you are responsible for managing the release of it.

For example if you create a String like this:

NSString *myTestString = [[NSString alloc] 
                initWithString:@"My Test String"];

Somewhere down the line you’ll need to make sure you release it, or you’ll be leaking memory.

[myTestString release];

If you’re worried about remembering the release you could also write the top part like this.

NSString *myTestString = [[[NSString alloc]
               initWithString:@"My Test String"] autorelease];

and then you won’t need to release it later on.

Hope that helps someone.

Posted: October 13th, 2008
Categories: Cocoa, Objective-C
Tags: ,
Comments: 3 Comments.