gpt4 book ai didi

iphone - Objective-C:NSMutableString replaceCharactersInRange 引发异常

转载 作者:太空狗 更新时间:2023-10-30 04:01:47 27 4
gpt4 key购买 nike

我希望这段代码能用一些爱来代替仇恨。

 NSMutableString *teststring=@"I hate programming my iPhone";

NSString *overwriteSource=@"love";

NSRange temprange=NSMakeRange(2, 4);

[teststring replaceCharactersInRange:temprange withString:overwriteSource];

NSLog(@"%@",teststring);

由于未捕获的异常而终止,我不明白为什么。帮助 ! (谢谢)

最佳答案

在第一行中,您试图将 NSString * (@"I hate...") 分配给 NSMutableString * (teststring)。

你的原始代码应该给你一个编译警告:

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

以下将使您的代码示例能够正确编译和运行:

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

关于iphone - Objective-C:NSMutableString replaceCharactersInRange 引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2035969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com