29

首先,很抱歉这个简单的问题。但我需要了解正在发生的事情。

我认为输出应该是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];
}

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

4

5 回答 5

63

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

做你想做的事情的一种方法是将指针传递给指针:

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

-(void) changeString:(NSString**)string
{
     *string = [*string lowercaseString];
}
于 2012-07-31T09:59:56.037 回答
9

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

返回接收者的小写表示。

- (NSString *)lowercaseString

您的代码所做的只是用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;
}

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

于 2012-07-31T10:02:46.683 回答
1

我指的是上面给出的问题并帮助你解决错误。找到我的评论

- (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
}
于 2016-10-20T14:03:33.267 回答
0

string in-(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];
}
于 2012-07-31T10:01:18.140 回答
0

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

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

于 2012-07-31T10:02:51.633 回答