Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何传递NSMutableString类型的参数_Objective C_Nsmutablestring - Fatal编程技术网

Objective c 如何传递NSMutableString类型的参数

Objective c 如何传递NSMutableString类型的参数,objective-c,nsmutablestring,Objective C,Nsmutablestring,我正在学习objective-c,当我试图将参数传递给接受NSMutableString类型数据的方法RequestExecuteCountletsForString时,我收到了一条警告 我按如下方式传递文本参数: [delegatee RequestExecuteCountLettersForString:@"XYZ"]; 我收到这个警告: Incompatible pointer types sending 'NSString *' to parameter of type 'NSMut

我正在学习objective-c,当我试图将参数传递给接受NSMutableString类型数据的方法RequestExecuteCountletsForString时,我收到了一条警告

我按如下方式传递文本参数:

[delegatee RequestExecuteCountLettersForString:@"XYZ"];
我收到这个警告:

Incompatible pointer types sending 'NSString *' to parameter of type 
'NSMutableString *'
请告诉我如何修复此警告,以及为什么我无法将此类文本输入传递给该方法。

文本@XYZ不是NSMutableString。这是一根线。这就是错误告诉你的

您需要传递一个实际的可变字符串

NSMutableString *str = [NSMutableString stringWithString:@"XYZ"]; // or [@"XYZ" mutableCopy];
[delegatee RequestExecuteCountLettersForString:str];
两件事:

为什么您的方法采用可变字符串?如果方法修改可变字符串以便调用者可以使用结果,那么更好的方法是传入不可变字符串,并让方法使用返回值返回新字符串。如果它接受可变字符串的唯一原因是因为该方法在内部进行了一些更改,但调用方不需要知道这些更改,那么您绝对不应该让调用方传入可变字符串。因此,无论哪种方式,使用NSMutableString参数而不是NSString参数都可能是不正确的。 方法名称应以小写字母开头。变量和属性也是如此。类名以大写字母开头。 [delegatee RequestExecuteCountLettersForString:[@XYZ mutableCopy]];