views:

84

answers:

1

I've been reading that if I have something like this:

@property (nonatomic, assign) UIView *anView;

Then I don't have to care about memory management. I don't have to do [anView release] in the -dealloc method.

Why?

"assign" just tells the compiler: "Hey man, this property does not retain whatever anyone assigns to it". And then you may do

@synchronize anView;

and the compiler magically creates a getter and setter, where the setter may just look like that:

- (UIView*) anView {
    return anView;
}

and the Setter may look like:

- (void) setAnView:(UIView*)newAnView {
    anView = newAnView;
}

right/wrong? If that was right, the setter claims no ownership of the newAnView variable, since it's not a "retain" property. It just takes it and assigns it to the instance variable. So when considering the ownership policy, there is no need for releasing the anView instance variable in the -dealloc. The object doesn't own it. Ist that correct?

+4  A: 

Yes, you're basically correct. Let's assume that the garbage collector is off (otherwise it doesn't matter anyway).

When using 'assign' properties, you don't have to release the object because you never retained it (it someone else's responsibility).

Essentially, this creates a weak reference -- anView may be deallocated while the property still points at it. Usually, you'd use 'assign' for primitive data types, not for objects.

thesamet
Thanks. a.f.a.i.k there is no GC on iPhone OS... don't know about 3.0, but in the earlier ones there's only reference counting around.
Thanks