@property, @synthesize of IBOutlet

Discussion in 'iOS Programming' started by cthesky, Oct 2, 2011.

  1. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #1
    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. :)
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    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.
     
  4. PhoneyDeveloper, Oct 2, 2011
    Last edited: Oct 2, 2011

    PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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).
     

Share This Page