因copy和mutableCopy与 强制类型转换,使用不当而产生的异常

导致出现错误的代码:

先说一下执行了那些代码才出现这样的错误的:

定义了这样一个 NSString 的对象:

NSString *resultString = nil;

然后 用 resultString 调用了一个方法:

[(NSMutableString *)resultString insertComma];

需要说明的是,insertComma 这个方法,是一个 NSMutableString 类的扩展方法。把 resultString 强制转换成 NSMutableString 类型,才能 调用 insertComma 这个方法。
insertComma 这方法 如下:

#import "NSMutableString+Deletion.h"

@implementation NSMutableString (Deletion)

- (void)insertComma {
    int count = 0;
    long long int a = self.longLongValue;
    while (a != 0)    {
        count++;
        a /= 10;
    }
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3) {
        count -= 3;
        NSRange rang = NSMakeRange(self.length - 3, 3);
        NSString *str = [self substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:@"," atIndex:0];
        [self deleteCharactersInRange:rang];
    }
    [newstring insertString:self atIndex:0];
    [self setString:newstring];

} // insertComma

然而,编译程序,一执行,就出现了错误:

控制台打印出来的错误:

控制台打印出来的错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'

图片 如下:
这里写图片描述


解决办法:

后来,我把 resultString 调用 insertComma 方法的代码,改成这样就没有错误了,编译器就完美通过啦。

NSMutableString *strResult = [resultString mutableCopy];
[strResult insertComma];
resultString = [strResult copy];

那么为什么会这样呢?

我的猜想:

我觉得,在[(NSMutableString *)resultString insertComma];这行代码中,resultString 在转换成 (NSMutableString *) 类型时,默认使用的是 copy 这个方法,而不是 mutableCopy 。也就是说 (NSMutableString *)resultString 默认转换成的是 NSString * 不可变的字符串对象。

补充:
copy 与 mutableCopy 这两个方法的区别是:
1、copy 这个方法是要 拷贝出一 个 不可变的 副本,不改变原始对象的可变性;
2、mutableCopy 这个方法是要拷贝出一个 可变的 副本,不改变原始对象的可变性。
如 :

NSMutableString *strResult = [resultString mutableCopy];
[strResult insertComma];
resultString = [strResult copy];

意思是 不可变的 resultString 拷贝出来一个 可变的 strResult ,然后,用 可变的 strResult 去调用 insertComma 方法,最后,可变的 strResult 拷贝出来一个 不可变的 对象,并把它 赋值 给 resultString 。

关于更多 copy 与 mutableCopy 的区别,请参考:
这里写链接内容
http://blog.csdn.net/fg313071405/article/details/16858787
http://www.cnblogs.com/foxmin/archive/2012/07/05/2577154.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值