iOS 字典轉陣列,陣列轉字典

weixin_34185560發表於2018-10-23

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;

}

相關文章