美文网首页
iOS 给参数用ascll排序

iOS 给参数用ascll排序

作者: 夏天爱西瓜汁 | 来源:发表于2018-03-27 13:33 被阅读22次

    /** 对字典(Key-Value)排序
     @param dict 要排序的字典
     */
    + (NSMutableDictionary *)sortedDictionary:(NSMutableDictionary *)dict {
        NSMutableString *contentString =[NSMutableString string];
        NSArray *keys = [dict allKeys];
        //排序
        NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [obj1 compare:obj2];
        }];
        //拼接 把排序后的字典拼接成string
        for (NSString *keyStr in sortedArray) {
            if (![[dict objectForKey:keyStr] isEqualToString:@""] && ![[dict objectForKey:keyStr] isEqualToString:@"key"] ) {
                [contentString appendFormat:@"%@=%@&", keyStr, [dict objectForKey:keyStr]];
            }
        }
        contentString = (NSMutableString *)[contentString substringToIndex:contentString.length-1];
        NSLog(@"%@", contentString);
        
        //加密 把拼接后的参数加密生产sign
        NSString *signStr = [[NSString stringWithFormat:@"%@%@", contentString, MX_KEY] MD5];
        [dict setObject:signStr forKey:@"sign"];
        
        return dict;
    }

    相关文章

      网友评论

          本文标题:iOS 给参数用ascll排序

          本文链接:https://www.haomeiwen.com/subject/cablcftx.html