开发者

iOS - viewDidUnload weird call

开发者 https://www.devze.com 2023-03-17 03:21 出处:网络
I\'ve been reading the \"Beginning iPhone 4 Development\" book and they have one tutorial for using navigation controllers. In this tutorial we push a view controller (named childController) onto the

I've been reading the "Beginning iPhone 4 Development" book and they have one tutorial for using navigation controllers. In this tutorial we push a view controller (named childController) onto the view. But since we don't want to "expose" this controller for other classes we have not created any accessor methods for it. So in the viewDidUnload the authors have written:

- (void)viewDidUnload {
    [childController release], childController = nil;
    [super viewDidUnload];
    // Release any retained subviews of开发者_如何学C the main view.
    // e.g. self.myOutlet = nil;
}

I really don't understand the comma sign after the release? This code is actually working for me. I haven't received any errors or memory leaks.

But to my understanding shouldn't it be like this instead:

- (void)viewDidUnload {
    [childController release]; 
    childController = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


The comma operator in C evaluates both its operands (they must be expressions, not statements), discarding the first operand and returning the value of the second operand. In your example:

[childController release], childController = nil;

the compiler does the following:

  1. Evaluate [childController release] (effectively sending -release to childController) and discard the return value (which, in the case of -release, is void);
  2. Evaluate childController = nil. This is an assignment expression that assigns the second operand (nil) to the first operand childController and returns the value of the left operand in the assignment;
  3. Return the value of the second operand (childController, which contains nil).

Since C allows the value of an expression not to be used, the value of [childController release], childController = nil (which is nil) is discarded.

In practical terms, this is equivalent to performing:

[childController release]; 
childController = nil;

but some programmers prefer to use the expression because of the tight relation between releasing the instance variable and assigning nil to it. It’s visually clear (and easy to spot errors, for instance forgetting the nil assignment); compare:

[childController release], childController = nil;
[someIvar0       release], someIvar0       = nil;
[someIvar1       release], someIvar1       = nil;
[someIvar2       release], someIvar2       = nil;

with:

[childController release]; 
childController = nil;
[someIvar0       release]; 
someIvar0       = nil;
[someIvar1       release]; 
someIvar1       = nil;
[someIvar2       release]; 
someIvar2       = nil;

It’s a matter of style.

Edit: Eiko has mentioned in a comment that the following, which does not use the comma operator, is another option:

[childController release]; childController = nil;
[someIvar0       release]; someIvar0       = nil;
[someIvar1       release]; someIvar1       = nil;
[someIvar2       release]; someIvar2       = nil;


 [childController release], childController = nil;

this is plain C, comma operator aimed at "sequencing" expressions. actually, in this case, it is completely equivalent to:

[childController release];
childController = nil;

the only thing that changes is readability (for worse or better, it depends...)

but in general the sequencing operator evaluates to the last statement's value (i.e., values of all operands are discarded except the last one).

int a, b;
int c = (a = 1), (b = 2);

c would be 2 after execution of this statement.

EDIT: from the link I added:

There's hardly any reason to use a comma operator anywhere other than in the first and third controlling expressions of a for loop, and in fact most of the commas you see in C programs are not comma operators. In particular, the commas between the arguments in a function call are not comma operators; they are just punctuation which separate several argument expressions. It's pretty easy to see that they cannot be comma operators, otherwise in a call like

edit: a better example:

int a = 1;
int b = 2;
b = printf("%d, %d", a,b), a+b;

this will first print:

1, 2

then assign 3 to b.

0

精彩评论

暂无评论...
验证码 换一张
取 消