Objective C - Pass by value and pass by reference

前端 未结 5 1524
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 23:22

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

相关标签:
5条回答
  • 2020-12-07 23:26

    string in -(void) changeString:(NSString*)string is local variable, modify method to return value:

    -(void) test
    {
         NSString *stringVar = @"UPPER CASE STRING";
         stringVar =[self changeString:stringVar];
         NSLog(@"value after changed : %@", stringVar);
    }
    -(NSString *) changeString:(NSString*)string
    {
        return [string lowercaseString];
    }
    
    0 讨论(0)
  • 2020-12-07 23:33

    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.

    0 讨论(0)
  • 2020-12-07 23:33

    string is a local variable (a pointer to an NSString which is immutable), you're just changing what stringpoints to in the local function, but when you return its value will be thrown away.

    What you may want to do is to simply pass the string as a parameter and return the lower case string from the function.

    0 讨论(0)
  • 2020-12-07 23:50

    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];
    }
    
    0 讨论(0)
  • 2020-12-07 23:50

    I am referring to above given problem and helping you with the mistake.Find my comments

    - (void)test
    {
         NSString *stringVar = @"UPPER CASE STRING";
         //StringVar is a pointer to integer class.let us assume the address the stringVar be 0x50 and the value it has be 0x100
         //So 0x100 has the string 
    
         [self changeString:stringVar];
         //above we are calling the method to lowercase and argument is stringVar
         //As the method is called we pass 0x100 in the argument as this is the memory where the value is kept
    
         NSLog(@"value after changed : %@", stringVar);
        //Our StringVar still points to 0x100 where the value is in upperString
    }
    
    - (void)changeString:(NSString*)string
    {
         string = [string lowercaseString];
        // Now operation performed on string returns a new value on suppose location 0x200
       //String parameter passed in argument is assigned the new value.But we never pass values as we pass only location under the hood
       //New parameter passed now points to new memory location 0x200
    
    }
    
    ---------------------------------------------------------------------------------
    With the new solution
    -(void) test
    {
         NSString *stringVar = @"UPPER CASE STRING";
         //let 0x50 be stringVar memorylocation pointing to 0x100 with above value
         [self changeString:&stringVar];
         //0x50 is passed in argument
         NSLog(@"value after changed : %@", stringVar);
         //On new location stored in address of stringVar it points to new string value
    }
    
    -(void) changeString:(NSString**)string
    {
         *string = [*string lowercaseString];
         //0x200 is new memory location received.*string gives the 0x100 location and hence the value
       //LHS is assigned to new location .On LHS you find *string which will be assigned 0x200
       //As string with location 0x50 is value 0x200 which will make it to point new      location where lowercase string exist
    }
    
    0 讨论(0)
提交回复
热议问题