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];