可变字符串用不了appendFormat 方法的时候

 昨天和楠哥调程序的时候 ,有遇到一个bug,说简单也简单,说复杂也复杂。 客户需要追加两个字段,而且是可选的字段,但是最终所有的字段合起来组成签名,按照
ASCII码表排序每个字段,

 [self.merStr2 appendFormat:@"merchantId=%@|""merchantOrderAmt=%@|"

                    "merchantOrderCurrency=%@|""merchantOrderDesc=%@|""merchantOrderId=%@|""merchantOrderTime=%@|""merchantUserId=%@|",self.merchantId,self.merchantOrderAmt,self.merchantOrderCurrency,self.merchantOrderDesc,self.merchantOrderId,self.merchantOrderTime,self.merchantUserId];



@property (nonatomic,copy)NSMutableString *merStr2;     

if (!self.merStr2) {

        self.merStr2 = [[NSMutableString alloc] initWithString:@""];//构建可变字符串对象,并且赋上初值。   

 }

这么写了之后 ,按道理就可以对self.merStr2   进行操作了  ,比如追加,插入,剔除。

appendFormat,

appendString,    

奇怪的是 appendFormat   这个方法居然用不了,是报错的,刚开始查找这个bug,以为是没有赋初值,或是分配空间不够,或是只能添加常量,不能添加变量(经过转换赋值的);

然后继续跟踪,就直接在 self.merStr2   后appendFormat  一个常量字符串@“aaaa”     结果还是报错 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendFormat:'
*** First throw call stack:              感觉好像是内存溢出,   其实当时就感觉到是不是self.merStr2 的空间被固定了,不可改变的,那要不然为什么定义的可变字符串 不能进行任何增删改除的操作呢,本来最初是@property (nonatomic,copy)NSString *merStr2;  的,  需要增加字段才改成NSMutableString类型。


后来瞎试了下,无意中把copy  改成strong  竟然一下子验签通过了,哈哈

其实真正的缘由是这样的,因为用copy了,其实是拷贝了一份,这一片内存是不可以操作的,但是用strong,只是引用。  用copy了之后,实际上是两个东西了,操作的已经不是原来的那一片了,恰好strong ,只是引用,操作的还是原来的那一份。      当用了copy之后,@property (nonatomic,copy)NSMutableString *merStr2;     实际跟@property (nonatomic,copy)NSString *merStr2; 没啥区别。(拷贝过来的内存已经固定)。


@property (nonatomic,strong)NSMutableString *merStr2;       //这个是对的

用了 appendFormat  这个方法后,就可以解决字符串签名的问题了,



 if (!self.merStr2) {

        self.merStr2 = [[NSMutableString alloc] initWithString:@""];


    }

    [self.merStr2 appendFormat:@"merchantId=%@|""merchantOrderAmt=%@|"

                    "merchantOrderCurrency=%@|""merchantOrderDesc=%@|""merchantOrderId=%@|""merchantOrderTime=%@|""merchantUserId=%@|",self.merchantId,self.merchantOrderAmt,self.merchantOrderCurrency,self.merchantOrderDesc,self.merchantOrderId,self.merchantOrderTime,self.merchantUserId];

//    self.merStr2=[NSString stringWithFormat:@"merchantId=%@|""merchantOrderAmt=%@|"

//                  "merchantOrderCurrency=%@|""merchantOrderDesc=%@|""merchantOrderId=%@|""merchantOrderTime=%@|""merchantUserId=%@|""misc=%@|""msgExt=%@|""notifyURL=%@|""signature=%@",self.merchantId,self.merchantOrderAmt,self.merchantOrderCurrency,self.merchantOrderDesc,self.merchantOrderId,self.merchantOrderTime,self.merchantUserId,self.model.misc,self.model.msgExt,self.notifyURL,self.signature];

    

    if (self.model.misc) {

        [self.merStr2 appendFormat:@"misc=%@|",self.model.misc];

    }

    if (self.model.msgExt) {

        [self.merStr2 appendFormat:@"msgExt=%@|",self.model.msgExt];

    }

    

    [self.merStr2 appendFormat:@"notifyURL=%@|""signature=%@",self.notifyURL,self.signature];

    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值