开发者

Objective-C: NSMutableString replaceCharactersInRange raising Exception

开发者 https://www.devze.com 2022-12-16 02:27 出处:网络
I expected this code to replace the hate with some love. NSMutableString *teststring=@\"I hate programming my iPhone开发者_C百科\";

I expected this code to replace the hate with some love.

 NSMutableString *teststring=@"I hate programming my iPhone开发者_C百科";

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

Your original code should give you a compilation warning:

incompatible Objective-C types initializing 'struct NSString *', expected 'struct NSMutableString *'

The following will allow your code sample to compile and run correctly:

NSMutableString *teststring=[NSMutableString stringWithString:@"I hate programming my iPhone"];
0

精彩评论

暂无评论...
验证码 换一张
取 消