NSDictionary 用法
阅读原文时间:2023年07月09日阅读:2

//Dictionary

//不可变

//字典里面:是以键值对存放的

//字典存放顺序是无序的

//字典里面:value可以重复,但key不能重复

//字典里面:key一般用字符串表示,value可用不同的对象来填充

NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan" , @"lisi" , @"zhangxiahua" , @"wangdahua" , @"zhaoxiaojie" , @"liuhuaixuan" ,  nil];

NSLog(@"%@" , dic1);

NSLog(@"%ld" , [dic1 count]);

//获取一个字典当中所有的key值

//存放了所有key值的array

NSArray *keyArray = [dic1 allKeys];

for (int i = 0; i < [dic1 count]; i++) {

NSLog(@"%@" , [dic1 objectForKey:[keyArray objectAtIndex:i]]);

//        NSLog(@"%@" , [dic1 objectForKey:[[dic1 allKeys] objectAtIndex:i]]);

}

NSDictionary *dic2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1" , @"value2" , @"key2" , @"value3" , @"key3" , nil];

NSLog(@"%@" , dic2);

//通过key数组和value数组, 快速创建字典

NSArray *keyArray1 = @[@"key1" , @"key2" , @"key3"];//OC数组方便写法

//        NSLog(@"%@" , keyArray);

NSArray *valueArray = @[@"value1" , @"value2" , @"value3"];

//        NSLog(@"%@" , valueArray);

NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray1];

NSLog(@"%@" , dic3);

NSMutableDictionary 是NSDictionary的子类,所以继承了NSDictionary的方法。

[NSMutableDictionary dictionaryWithCapacity:1] : 创建一个可变词典初始指定它的长度为1.,动态的添加数据如果超过1这个词典长度会自动增加,所以不用担心数组越界。推荐用这种方式

[NSMutableDictionary initWithCapacity:10]  :只是初始化一个词典的长度为10。

[dictionary setObject:@"taotao" forKey:@"name"] :向可变的词典动态的添加数据 ,这里的key是name ,值是taotao如果词典中存在这个KEY的数据则直接替换这个KEY的值。(易混的地方,慎重!)

[dictionary removeAllObjects..] : 删除掉词典中的所有数据。

[dictionary removeObjectForKey..] :删除掉词典中指定KEY的数据 。

//字典快速写法

NSDictionary *dic4 = @{@"key1":@"value1" , @"key2":@"value2" , @"key3":@"value"};

NSLog(@"%@" , dic4);

Person *p1 = [Person personWithName:@"ren"];

Student *s2 = [Student personWithName:@"xues"];

Teacher *t3 = [Teacher personWithName:@"sdhdsjh"];

NSDictionary *dic = @{@"key1":p1 , @"key2":s2 , @"key3":t3};

NSLog(@"%@" , dic);

常用方法

获取字典数量

NSInteger count = [dic count];

通过key获取对应的value对象

NSObject *valueObj = [dic objectForKey:@"key"];

将字典的key转成枚举对象,用于遍历

NSEnumerator *enumerator = [dic keyEnumerator];

获取所有键的集合

NSArray *keys = [dic allKeys];

获取所有值的集合

NSArray *values = [dic allValues];

初始化一个空的可变字典

NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"key1",@"v2",@"key2",nil];

NSDictionary *dic3 = [NSDictionary dictionaryWithObject:@"v3" forKey:@"key3"];

向字典2对象中添加整个字典对象3

[dic2 addEntriesFromDictionary:dic3];

向字典2对象中最佳一个新的key3和value3

[dic2 setValue:@"value3" forKey:@"key3"];

初始化一个空的可变字典

NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

将空字典1对象内容设置与字典2对象相同

[dic1 setDictionary:dic2];

将字典中key1对应的值删除

[dic1 removeObjectForKey@"key1"];

NSArray *array = [NSArray arrayWithObjects:@"key1", nil];

根据指定的数组(key)移除字典1的内容

[dic2 removeObjectsForKeys:array];

移除字典所有对象

[dic1 removeAllObjects];

//创建一个可变字典

// 用指定空间大小来初始化

NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] initWithCapacity:1];

Person *p1 = [Person personWithName:@"hdilo" andAge:23];

Person *p2 = [Person personWithName:@"hsdsf" andAge:13];

Person *p3 = [Person personWithName:@"bhycu" andAge:56];

Person *p4 = [Person personWithName:@"yusyt" andAge:67];

Person *p5 = [Person personWithName:@"yhudh" andAge:345];

//增

//从一个字典当中拿出放到别的字典里

//[mdic1 addEntriesFromDictionary:mdic2];

[mdic1 setValue:p1 forKey:@"pingguo"];

[mdic1 setValue:p2 forKey:@"dage"];

[mdic1 setValue:p3 forKey:@"haha"];

[mdic1 setValue:p4 forKey:@"hehe"];

//删

[mdic1 removeObjectForKey:@"dage"];

//按照一组key值进行删除

//用mutableCopy方法,将一个不可变的东西转换成一个可变的东西

//NSMutableDictionary *allkey = [[mdic1 allKeys] mutableCopy];

//改

//当给一个字典设定一对值的话 ,如果字典当中已经存在这个key值,就直接把原来key值当中对应的元素替换掉

//如果没有对应key值,就增加这一对设定的值

[mdic1 setValue:p5 forKey:@"hehe"];

[mdic1 setValue:p5 forKey:@"dede"];

//查

//根据key来查找一个value

Person *p6 = [mdic1 objectForKey:@"haha"];

NSLog(@"%@" , p6);

NSLog(@"%@" , mdic1);

遍历字典

快速枚举

for (id key in dic){

id obj = [dic objectForKey:key];

NSLog(@"%@", obj);

}

一般枚举

NSArray *keys = [dic allKeys];

inr length = [keys count];

for (int i = 0; i < length;i++){

id key = [keys objectAtIndex:i];

id obj = [dic objectForKey:key];

NSLog(@"%@", obj);

}

通过枚举类型枚举

NSEnumerator *enumerator = [dic keyEnumerator];

id key = [enumerator nextObject];

while (key) {

id obj = [dic objectForKey:key];

NSLog(@"%@", obj);

key = [enumerator nextObject];

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章