1.下单的时候,用于签名的原始串是一个参数的集合,有时候需要我们进行排序。假如用于签名的字段有,name,age,sex,三个字段。我们可以在下单接口中先定义一个字典,将数据传进来即可。
//我们用于排序的字段
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Danny",@"name",@"12",@"age",@"man",@"sex",nil];
2.逻辑:通过取出字典的所有key值,利用sortedArrayUsingComparator进行排序,通过排序后的key值取value,从而达到排序的目的。
3.&&&上代码啦啦。。。
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Danny",@"name",@"12",@"age",@"man",@"sex",nil];
NSArray *allKeyArray = [dict allKeys];
NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSComparisonResult resuest = [obj2 compare:obj1];
return resuest;
}];
NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
//通过排列的key值获取value
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortsing in afterSortKeyArray) {
NSString *valueString = [dict objectForKey:sortsing];
[valueArray addObject:valueString];
}
NSLog(@"valueArray:%@",valueArray);
3.打印log信息:
log:
afterSortKeyArray:(
sex,
name,
age
)
valueArray:(
man,
Danny,
12
)
4.接下来我们就可以进行我们签名参数的排列了,生成signString后可以进行MD5加密。
NSMutableArray *signArray = [NSMutableArray array];
for (int i = 0 ; i < afterSortKeyArray.count; i++) {
NSString *keyValue = [NSString stringWithFormat:@"%@=%@",afterSortKeyArray[i],valueArray[i]];
[signArray addObject:keyValue];
}
//signString用于签名的原始参数集合
NSString *signString = [signArray componentsJoinedByString:@"&"];
NSLog(@"signString:%@",signString);
打印log:
signString:sex=man&name=Danny&age=12