ios 目标 C - 按值传递和按引用传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11737505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:30:26  来源:igfitidea点击:

Objective C - Pass by value and pass by reference

iosobjective-cstringparameter-passing

提问by Charith Nidarsha

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.

我认为输出应该是upper case string. 但结果是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?

发生了什么以及如何解决?

回答by Greg Hewgill

The [string lowercaseString]call creates a newNSStringobject that you assign to the local variable string. This does not change the value of stringVaroutside the changeStringfunction. The pointer itself is passed by value.

[string lowercaseString]调用会创建一个您分配给局部变量的NSString对象string。这不会改变函数stringVar外部的changeString值。指针本身是按值传递的。

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];
}

回答by trojanfoe

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:

如果您查看对[NSString lowerCaseString]方法的引用,您会发现它返回一个新字符串,其中包含小写字符:

Returns lowercased representation of the receiver.

- (NSString *)lowercaseString

返回接收者的小写表示。

- (NSString *)lowercaseString

What your code does is simply overwrite the reference to the input string with the output of the lowercaseStringcall, 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:

您的代码所做的只是用lowercaseString调用的输出覆盖对输入字符串的引用,这没有任何效果。解决这个问题最好的方法是你自己返回值,这样方法更容易理解:

-(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 NSStringis immutable so there is no way, other than reassigning the reference, to change a string's contents. You could, however use NSMutableStringinstead, which can be modified in place.

您需要了解它NSString是不可变的,因此除了重新分配引用之外,没有其他方法可以更改字符串的内容。但是NSMutableString,您可以改为使用,可以就地修改。

回答by Suryanarayan Sahu

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
}

回答by Joachim Isaksson

stringis 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.

string是一个局部变量(指向不可变的 NSString 的指针),您只是更改string了局部函数中指向的内容,但是当您返回它的值时,它的值将被丢弃。

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

您可能想要做的是简单地将字符串作为参数传递并从函数返回小写字符串。

回答by mas'an

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

字符串输入-(void) changeString:(NSString*)string是局部变量,修改方法返回值:

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