解释"警告:从不同的Objective-C类型分配"

rso*_*son 0 string objective-c

如果我将可变字符串的值设置为数组中的值,并使用以下代码来操作它:

NSMutableString *theCountry = [listItems objectAtIndex:3];
theCountry = [theCountry stringByReplacingOccurrencesOfString:@"\"" withString:@""];
Run Code Online (Sandbox Code Playgroud)

我在上面代码的第二行之后收到警告"警告:从不同的Objective-C类型分配".如果我在方法调用之前没有"theCountry =",则警告消失,但字符串不会被操纵...

Meh*_*ari 5

stringByReplacingOccurrencesOfString:withString:声明该方法返回一个NSString*而不是一个NSMutableString*.基本上,在赋值中,您要NSString*为一个NSMutableString*不一定安全的类型变量赋值(注意NSMutableString继承NSString,而不是相反).