iOS开发中字典排序并转换成字符串的方法;

开发中经常涉及到用把参数拼接成字符串用MD5加密的方式做一个标识符,以下是字典用key以升序的方式排列并转成字符串,并用递归的方式把value中的字典也转成字符串:

-(NSString*)stringWithDict:(NSDictionary*)dict{

NSArray*keys = [dictallKeys];

NSArray*sortedArray = [keyssortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2) {

return[obj1compare:obj2options:NSNumericSearch];

}];

NSString*str =@"";

for(NSString*categoryIdinsortedArray) {

idvalue = [dictobjectForKey:categoryId];

if([valueisKindOfClass:[NSDictionaryclass]]) {

value = [selfstringWithDict:value];

}

//NSLog(@"[dict objectForKey:categoryId] === %@",[dict objectForKey:categoryId]);

if([strlength] !=0) {

str = [strstringByAppendingString:@";"];

}

str = [strstringByAppendingFormat:@"%@:%@",categoryId,value];

}

returnstr;

}

你可能感兴趣的:(iOS开发中字典排序并转换成字符串的方法;)