Quote:
|
I thought autorelease marked the item for garbage collection at the later time, and GC is not available on the iphone.
|
not quite. autorelease just marks an object for a release action "at some point in the future." the future point is typically the next time the run loop is entered, which is usually pretty quick. (i'm qualifying this heavily with words like "typically" and "usually" because you can do things to interfere with this, but most of the time you won't.)
garbage collection, the way most people think of it, is completely automatic and happens outside the control of the programmer. autorelease is under your control.
my advice would be to only use autorelease when you are returning an object that you alloced. eg:
Code:
- (someobject *)myMethodReturningObject {
someobject *myobject = [[someobject alloc] init];
[myobject doSomethingToSetItUp];
[myobject autorelease];
return myobject;
}
As you can see here, you've alloced the object, so it is your responsibility to provide a balancing release message. However, you want to return it to another method, so you can't just release it, or it would be lost before the other method got it. Autorelease is your friend in this case.
Quote:
|
I'm coming from programming ruby the last couple of years - life used to be so easy....
|
tell me about it - i was doing perl and php for the last 10 years. cocoa was quite a kick in the pants, took me about 6-8 weeks to get comfortable with it.