iOS 字典转数组,数组转字典

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]];
//数组升序排列
[dicToArray sortUsingSelector:@selector(compare:)];

}];

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;

}

你可能感兴趣的:(iOS 字典转数组,数组转字典)