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?
Objective-C Language Methods Pass by value parameter passing So actual parameter value will not change after returning from called function.
"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.
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.
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.
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]; }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With