[英]Objective-C: NSMutableString replaceCharactersInRange raising Exception
I expected this code to replace the hate with some love. 我希望这段代码可以用一些爱来代替仇恨。
NSMutableString *teststring=@"I hate programming my iPhone";
NSString *overwriteSource=@"love";
NSRange temprange=NSMakeRange(2, 4);
[teststring replaceCharactersInRange:temprange withString:overwriteSource];
NSLog(@"%@",teststring);
This terminates due to an uncaught exception and I can't figure out why. 由于未捕获的异常而终止,我无法弄清楚原因。 Help ! 救命 ! (and thanks) (并谢谢)
In your first line, you're attempting to assign an NSString * (@"I hate...") to an NSMutableString * (teststring). 在第一行中,您尝试将NSString *(@“我讨厌...”)分配给NSMutableString *(teststring)。
Your original code should give you a compilation warning: 您的原始代码应该为您提供编译警告:
incompatible Objective-C types initializing 'struct NSString *', expected 'struct NSMutableString *' 不兼容的Objective-C类型初始化'struct NSString *',期望'struct NSMutableString *'
The following will allow your code sample to compile and run correctly: 以下将允许您的代码示例编译并正确运行:
NSMutableString *teststring=[NSMutableString stringWithString:@"I hate programming my iPhone"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.