字典转数组 数组转字典
Posted 栗子Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字典转数组 数组转字典相关的知识,希望对你有一定的参考价值。
NSMutableArray *dicToArray = [NSMutableArray array];
NSMutableDictionary *arrayToDic = [NSMutableDictionary dictionary];
NSDictionary *dic = @{@"1":@"one",@"2":@"two",@"3":@"three"};
//字典转数组
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[dicToArray addObject:[NSString stringWithFormat:@"%@:%@",key,obj]];
}];
NSLog(@"dicToArray====%@",dicToArray);
//数组转字典
[dicToArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"obj===%@===idx===%lu",obj,(unsigned long)idx);
NSArray *array = [obj componentsSeparatedByString:@":"];//分割字符串
[arrayToDic setObject:array[1] forKey:array[0]];
}];
NSLog(@"arrayToDic====%@",arrayToDic);
dicToArray====(
"1:one",
"2:two",
"3:three"
)
arrayToDic===={
1 = one;
2 = two;
3 = three;
}
以上是关于字典转数组 数组转字典的主要内容,如果未能解决你的问题,请参考以下文章