startuprob
startuprob

Reputation: 1915

iPhone Objective-C self question

In a simple UITableView I have:

- (void)viewDidLoad {
  NSArray *array = [[NSArray alloc]initWithObjects:@"Pictures",@"Video",@"Text",@"Map",nil];
  self.selectionList = array;
  [array release];

  [super viewDidLoad];
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }

  NSUInteger row = [indexPath row];
  cell.textLabel.text = [selectionList objectAtIndex:row];

  return cell;
}

My question is this, why in the viewDidLoad section, does it need to be self.selectionList and not just selectionList?? I mean, the whole point is to pass the contents of the array we just created into the selectionList array, so why the self?

Upvotes: 0

Views: 174

Answers (5)

Kevin Sylvestre
Kevin Sylvestre

Reputation: 38012

Whenever you use 'self' it will use the property setter and getter. That means that if you create a property as being retain it will allow you to release (as the setter will call a retain). If you didn't use the setter (I'm assuming that it is a retain property), you could also write the above like this (but I wouldn't recommend it):

- (void)viewDidLoad 
{
  [super viewDidLoad];
  selectionList = [[NSArray alloc]initWithObjects:@"Pictures",@"Video",@"Text",@"Map",nil];
}

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

Upvotes: 1

Eiko
Eiko

Reputation: 25632

Using the dot-syntax for the (most likely) rataining or copying property has three effects: It assigns or copies the array (depending on the declaration), makes sure it gets a retain message so that it will stay alive, and - also important - releases the old array so that it doesn't leak.

If you wanted to go without the dot syntax, then you'd have to do all these steps manually.

Upvotes: 1

nils
nils

Reputation: 628

Because self.selectionList uses a property (-> setter method) instead of directly accessing the instance variable. This makes it possible to overwrite this variable access in a subclass what would not be possible with direct ivar access. Using properties is in general the cleaner approach (data encapsulation/information hiding).

As mentioned in the other comments: Without the property you have to care about memory management at every point you set the ivar. What will in this case properly mean that you have to drop the release.

Upvotes: 1

John Parker
John Parker

Reputation: 54445

In this instance, it needs to be self.selectionList, as this is using a setter method (I presume you've used a property with the retain attribute in the header and then synthesied selectionList in the implementation). It's effectively the same as doing...

[self setSelectionList:array]; 

...if that makes sense.

Otherwise, you'd simply be assigning the array to a class instance variable and you'd need to do a retain and subsequent release to ensure that it was managed correctly.

Upvotes: 0

James J
James J

Reputation: 6458

The difference here is how the memory management works. Assuming the @property selectionList is defined as retain, calling self.selectionList = something; implicitly calls [selectionList retain].

In this specific example, doing simply selectionList = something; would most likely cause a crash down the line, since the object is being released.

Upvotes: 0

Related Questions