Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Pass by value and pass by reference

First of all, sorry for this simple question. But I need to understand what is happening.

I thought the output should be upper case string. But it comes out to be UPPER CASE STRING.

- (void)test {      NSString *stringVar = @"UPPER CASE STRING";      [self changeString:stringVar];      NSLog(@"value after changed : %@", stringVar); }  - (void)changeString:(NSString*)string {      string = [string lowercaseString]; } 

What is happening and how to fix it?

like image 382
Charith Nidarsha Avatar asked Jul 31 '12 09:07

Charith Nidarsha


People also ask

Is Objective C pass by value or reference?

Objective-C Language Methods Pass by value parameter passing So actual parameter value will not change after returning from called function.

What is pass by value and pass by reference in C?

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.

What is function explain pass by value and pass by reference?

The difference between pass-by-reference and pass-by-value is that modifications made to arguments passed in by reference in the called function have effect in the calling function, whereas modifications made to arguments passed in by value in the called function can not affect the calling function.

Does C have pass by reference?

Pass by Reference A reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are ALSO MADE TO THE ORIGINAL variable. Arrays are always pass by reference in C.


2 Answers

The [string lowercaseString] call creates a new NSString object that you assign to the local variable string. This does not change the value of stringVar outside the changeString function. The pointer itself is passed by value.

One way to do what you want, is to pass a pointer to a pointer:

-(void) test {      NSString *stringVar = @"UPPER CASE STRING";      [self changeString:&stringVar];      NSLog(@"value after changed : %@", stringVar); }  -(void) changeString:(NSString**)string {      *string = [*string lowercaseString]; } 
like image 153
Greg Hewgill Avatar answered Oct 14 '22 17:10

Greg Hewgill


If you look at the reference to the [NSString lowerCaseString] method, you can see that it returns a new string, with the lowercase'd characters:

Returns lowercased representation of the receiver.

- (NSString *)lowercaseString 

What your code does is simply overwrite the reference to the input string with the output of the lowercaseString call, which has no effect. The best way to solve this issue is for you to return the value yourself, which makes the method easier to understand:

-(void) test {      NSString *stringVar = @"UPPER CASE STRING";      stringVar = [self changeString:stringVar];      NSLog(@"value after changed : %@", stringVar); }  -(NSString *) changeString:(NSString*)string {      string = [string lowercaseString];      return string; } 

You need to understand that NSString is immutable so there is no way, other than reassigning the reference, to change a string's contents. You could, however use NSMutableString instead, which can be modified in place.

like image 37
trojanfoe Avatar answered Oct 14 '22 18:10

trojanfoe