Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cthesky

macrumors member
Original poster
Aug 21, 2011
91
0
Hi,

I have some questions about "@property" and "@synthesize". Below is two sample codes.

Sample code 1 (with synthesize IBOutlet and instance variable).

ChangeText.h

@interface ChangeText : UIViewController {
UIButton * btn;
IBOutlet UITextField * txtField;

}

@property(nonatomic, retain) UIButton * btn;
@property(nonatomic, retain) IBOutlet UITextField * txtField;


-(IBAction)changeTxt : (id)sender;

@end

ChangeText.m

@implementation ChangeText

@synthesize btn;
@synthesize txtField;


-(void)changeTxt : (id)sender
{
txtField.text = @"Testing";
}

@end

But when I removed the @property and @synthesize. It still work fine as sample code 1. So, in my case, is it really a need to use @property and @synthesize ? What is the differences between add and remove @property and @synthesize in our code ? What is the effects of add @property and @synthesize to our code ? Below is the code with removed @property and @synthesize.


Sample code 2 (without synthesize IBOutlet and instance variable).

ChangeText.h

@interface ChangeText : UIViewController {
UIButton * btn;
IBOutlet UITextField * txtField;

}

-(IBAction)changeTxt : (id)sender;

@end

ChangeText.m

@implementation ChangeText

-(void)changeTxt : (id)sender
{
txtField.text = @"Testing";
}

@end

Hope someone can reply me. Thanks for your help. :)
 
These two code samples are not equivalent. Your second code sample is equivalent to a synthesized declared property with the assign attribute.

In the first code sample, the retain attribute causes @synthesize to add some extra functionality to setTxtField:. Which is, to release the previous object set to txtField and retain the new object being set to txtField. The Declare Properties chapter of The Objective-C Programming Language guide has the full details.

In iOS (unlike Mac OS X), objects unarchived from a xib file are created with a retain count of 1 and are then autoreleased. So the first code sample is correct from a memory-management point of view. With the second code sample, you risk holding a reference to a released object.

You might find the second code sample still works, because the view has been retained elsewhere. But that doesn't change the fact the second code sample contains a bug.
 
There are many, many web sites that answer this question already (just google "iPhone iboutlet retain"), but I'll summarize:

IBOutlets are automatically retained by their parent view, and will be automatically released under certain circumstances (if they have no parent view, or in response to a low memory warning). You need to retain them in order to safely access them from your own code, and the best way to do this is using a retained property.
 
The rules for how IBOutlets are managed are a little complicated. Using the retaining @property makes clear your intention and makes it less necessary to understand those rules.

Your risk in not having the @property is a memory leak if you don't release the IBOutlet in the correct places.

Beginners should certainly use the @property as all the sample code does and should also release those @properties in the correct places as all the sample code does. There is a lot of misunderstanding of this issue. Ignore all that and just use the retaining property. Someday if you move on from being a beginner you can try to learn the whole story (although it will have changed by then).
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.