Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - When to use 'self'

This is unmodified code from Apple's iPhone 'Utility Aplication' template:

- (void)applicationDidFinishLaunching:(UIApplication *)application {   MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];  self.mainViewController = aController;  [aController release];   mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;  [window addSubview:[mainViewController view]];  [window makeKeyAndVisible];  } 

When mainViewController is assigned to aController, the self keyword is specified:

 self.mainViewController = aController; 

However, when the mainViewController's frame is set, the self keyword is not required:

 mainViewController.view.frame = [UIScreen mainScreen].applicationFrame; 

If I remove the self keyword from the first example, the program crashes with the message:

objc[1296]: FREED(id): message view sent to freed object=0x3b122d0 

If I add the self keyword to the second example, the program runs fine.

Can anyone explain why self is needed in the first case but not the second? I'm assuming that in both cases mainViewController is referring to the same instance variable.

like image 295
Joe Avatar asked Mar 05 '10 10:03

Joe


People also ask

Is self a keyword in C?

this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity of which the currently running code is a part.

What is Self in C?

A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers pointing to the same type of structure as their member.

What is self in iOS?

self is a property on the instance that refers to itself. It's used to access the class, structure and enumeration instance within methods. When a method parameter has the same name as an instance property, you have to explicitly use self.

Is Objective-C as fast as C?

Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature.


1 Answers

Using self causes your class' "setter" for this variable to be called, rather than changing the ivar directly.

self.mainViewController = aController; 

is equivalent to:

[self setMainViewController:aController]; 

On the other hand:

mainViewController = aController; 

just changes the mainViewController instance variable directly, skipping over any additional code that might be built into UIApplication's setMainViewController method, such as releasing old objects, retaining new ones, updating internal variables and so on.

In the case where your accessing the frame, you're still calling a setter method:

mainViewController.view.frame = [UIScreen mainScreen].applicationFrame; 

expands to:

[[mainViewController view] setFrame:[[UIScreen mainScreen] applicationFrame]]; 

Ideally, to future proof your code, you should also be using self.mainViewController (or [self mainViewController]) when retrieving this value too. In general, classes are much less likely to have important code in their "getter" methods than their "setters", but it's still possible that accessing directly could break something in a future version of Cocoa Touch.

like image 121
Tobias Cohen Avatar answered Sep 29 '22 11:09

Tobias Cohen