3

I a few simple questions to make sure I am using properties right in my application. I read a lot online but it is still unclear. Thanks a lot for any help or suggestions.

(1) I am not quite sure that the statement does, and why is it needed.

@synthesize personName = _personName; 

Why do you need the _personName variable? What is the benefit of doing that as opposed to just creating a property and synthesizing that variable personName.

@property (nonatomic, retain) NSString *personName;

(2) In my application should I be accessing the property variable self.personName or use the _personName variable. I believe the self.personName is correct by then again why is the _personName even there?

(3) Also I am a little confused to which variable I should release in dealloc() and which variable should I set to nil in viewDidLoad(). I also do not know if any changes should be made to the didReceiveMemoryWarning() method.

@interface ViewController : UIViewController
{
    NSString *_personName;
}

@property (nonatomic, retain) NSString *personName;

@end



@implementation ViewController

@synthesize personName = _personName;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.personName = [[NSString alloc] initWithString:@"John Doe"];

    NSLog(@"Name = %@", self.personName);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc
{
    [super dealloc];
}

@end
Vikings
  • 2,527
  • 32
  • 45
  • For the last question: http://stackoverflow.com/questions/9276136/what-is-the-need-of-assigning-nil-after-releasing-an-object?answertab=active#tab-top – Manlio Mar 11 '12 at 17:32
  • Awesome, I took that out since that post was helpful. – Vikings Mar 11 '12 at 17:34
  • 1
    possible duplicate of [Synthesized property and variable with underscore prefix: what does this mean?](http://stackoverflow.com/questions/6049269/synthesized-property-and-variable-with-underscore-prefix-what-does-this-mean) – jscs Mar 11 '12 at 18:54
  • +1 for systematic, neat question..:-) – rohan-patel Mar 11 '12 at 20:03

1 Answers1

5
@synthesize personName = _personName;
  1. That statement creates the accessor methods for the personName property. You've specified that the accessors should use an instance variable named _personName. If you just had @synthesize personName;, the accessors would use personName as the instance variable instead.

  2. You should usually use the accessor methods, as in self.personName or somePerson.personName or somePerson.personName = @"Joe";. If you don't care what the name of the ivar that backs up the personName property, you don't need to specify it.

  3. Use the accessors in -viewDidLoad, as in: self.personName = nil;. Same for -didReceiveMemoryWarning:. Whether to use the ivar or property in -dealloc is debatable, and to some degree a matter of taste. The main concern with using the property accessors in -dealloc is that it can cause problems if your class is subclassed and the accessors are overridden. Often, you don't need to worry about that because you know that your class won't be subclassed.

  4. Setting an ivar to nil after releasing is also debatable. Many people feel that it's good style to do so, others feel like it's a waste of time. Use your best judgement. It's certainly not necessary, but rather something that some feel is a matter of good housekeeping.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I'm still slightly confused on the _personName, it seems like it is unnecessary, but I see some many examples do this including Apple's example. – Vikings Mar 11 '12 at 17:45
  • Also, should I ever release _personName in dealloc? or both self.personName and _personName? – Vikings Mar 11 '12 at 17:48
  • @Vikings1201 Using the underscore makes it much easier to visually spot those locations where the ivar is accessed directly (which should in almost all cases only be in the custom accessor methods) –  Mar 11 '12 at 17:58
  • 1
    @Vikings1201 See this question for further details: http://stackoverflow.com/questions/6049269/synthesized-property-and-variable-with-underscore-prefix-what-does-this-mean –  Mar 11 '12 at 18:04