字典转数组 数组转字典

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;

}

时间: 2025-01-11 09:48:47

字典转数组 数组转字典的相关文章

ios开发数组排序(数组中包括字典)

iphone开发数组排序(数组中包括字典) 1.普通数组排序: NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"0",@"8",@"6",@"1",nil]; NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){ if ([obj

HDU 4825 Xor Sum(二进制的字典树,数组模拟)

题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int node[3011111][2]; int tag,m,n,cas=0,T; long long one[64],allone,tmp; //0/1树的加数据操作,增加一个32的数 //其中如果当前位是0,则加左儿子,

字典value为数组

1.添加元素的时候,先取出数组,再向数组中添加元素: - (void)addPeason:(AddressPeason *)peason { if (![[peason getName] isEqualToString:nil]) { for (NSString *string in [_peasonDictionary allKeys]) { if ([[[peason getName] substringToIndex:1] isEqualToString:string]) { [[_pea

iphone开发数组排序(数组中包括字典)

原文地址 http://blog.csdn.net/bihailantian1988/article/details/8433812 iphone开发数组排序(数组中包括字典) 1.普通数组排序: NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"0",@"8",@"6",@"1",nil]; NSArray *sortedArray = [arr sortedA

指针数组??数组指针的区别.xml

pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-weight:bold;} .selfFuc{color:#800080;} .bool{color:#d2576f;} .condition{color:#000080;font-weight:bold;} .key{color:#000080;} .

利用runtime,避免UIButton 重复点击, 可变数组和可变字典为nil,或者数组越界导致的崩溃

Demo链接: https://github.com/ShaoWenLe/Runtimer-Demo.git 参考文章: http://www.jianshu.com/p/080a238c62b9 相关Runtime介绍: http://www.cocoachina.com/ios/20160523/16386.html http://www.cocoachina.com/ios/20160628/16843.html 1 #import <Foundation/Foundation.h> 2

指针数组,数组指针,指针函数,函数指针

1.指针数组:顾名思义,指针数组就是数组里面元素都是指针,代码如下: #include <stdio.h> int main(int argc, const char * argv[]) { // insert code here... //printf("Hello, World!\n"); int a=1,b=2;//先定义两个变量 int *p[]={&a,&b};//然后定义数组,数组元素都是指针 printf("%p\n",p[

iOS开发——根据数组中的字典中的某一元素排序

数组中的元素是字典,字典中的某一个元素,比如说姓名,现在需要按照姓名的首字母来排序,怎么搞? 做法很简单,在字典中加一个元素,保存姓名的首字母,然后用下面的方法排序. - (void)sortWifiList { NSArray *resultArray = [myDataArray sortedArrayUsingFunction:compare context:NULL]; [myDataArray removeAllObjects]; [myDataArray addObjectsFrom

swift基本用法-for循环遍历,遍历字典,循环生成数组

// Playground - noun: a place where people can play import UIKit //------------------------------------------------------------------------------ // 1. for // 传统的for循环方式在swift中同样支持 var num = 0 for(var i = 0; i < 10 ; i++) { num += i } num //---------

OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法

<span style="font-size:24px;">//////第一种方法 // NSMutableArray *arr = [NSMutableArray array]; // for (int i = 0; i < 13; i ++) { // [arr addObject:[NSString stringWithFormat:@"lanou%d",i + 1]]; // } // NSLog(@"%@",arr);