tags:

views:

116

answers:

2

When an autorelease string is assigned to an IBOutlet property, Is it getting retained. Like lets say I have an property

@property(nonatomic, retain) IBOutlet UILabel *lblName;

Now in my viewWillAppear I assign lblName a string as:

lblName = [NSString stringWithFormat: @"NameString"];

So what is the retain count for this string, do I need to make sure I assign lblName = nil; before assining it a new string everytime view is being added or viewWillAppear is invoked.

Also another question is about UIImageView

@property(nonatomic, retain) IBOutlet UIImageView *imgView;

Now when I use animation as

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

//some images are added to imageArray

imgView.animationImages = imageArray //NSMutableArray of autoreleased images.

[imageArray release];

are the images in that array are retained or is this array is being retained, since imgView is having a retain property.

A: 

When using properties with the retain attribute, you don't use the simple assignment.

You need to do the following:

@property(nonatomic, retain) NSString *someString;
@synthesize someString;

-(void)someMethod
{
    // either this
    [self setSomeString:@"Oh hai!"];

    // or this
    self.someString = @"Oh hai!";

    // but never this
    someString = @"Oh hai!";
}

The first two versions call the method setSomeString: but the third one doesn't. It merely assigns the pointer of @"Oh hai!" to the instance variable someString. This is a problem because it won't call [someString retain] but when finished, it will call release and cause an exception to occur because you're releasing it more than it was retained (since it wasn't retained when you assigned it).

For more information on memory management in iPhone look at this.

By the way, the IBOutlet macro actually evaluates to an empty macro. It is merely used by Interface Builder to flag a property as usable in a NIB.

Nick Bedford
A: 

I believe you mean: iblName.text = [NSString stringWithFormat:@"NameString"];

stringWithFormat: Will return an autoreleased string, so the retain count starts at one, assigning it to the text property will increment it to too but, since it is autoreleased, it will go back to one the next time the run loop drains the autorelease pool.

When you set it, the label will release the string and retain the new one, so you should be fine.

In the second case, the array will retain the autoreleased images, and setting animationImages will retain the array.

Elfred
Sorry I dint get you about the array release. Like Do I need to release the imageArray one more time, and do I need to relase the autoreleased images also.
rkb