OC语言 - 字典 | 集合
NSDictionary
1 - 字典的特性:① 只能存储对象;② 无序存储;③ key 唯一性
2 - 代码示例
① 如何使用 NSDictionary
1 // 方式一:创建 2 NSDictionary *dic01 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Hazell",@"name",@"female",@"sex",@"26",@"age", nil]; 3 NSLog(@"%@",dic01); 4 5 // 方式二:创建 6 NSDictionary *dic02 = [NSDictionary dictionaryWithObjectsAndKeys:@"Jane",@"name",@"female",@"sex",@"22",@"age", nil]; 7 NSLog(@"%@",dic02); 8 9 // 方式三:可以配合数组实现字典的创建 10 NSArray *values01 = [NSArray arrayWithObjects:@"Solina",@"female",@"18", nil]; 11 NSArray *keys01 = [NSArray arrayWithObjects:@"name",@"sex",@"age", nil]; 12 NSDictionary *dic03 = [NSDictionary dictionaryWithObjects:values01 forKeys:keys01]; 13 NSLog(@"%@",dic03); 14 15 // 键值对个数 16 NSLog(@"%ld",[dic03 count]); 17 // 通过 key 来索引数据 18 NSString *name01 = [dic03 objectForKey:@"name"]; 19 // 获取字典中所有的 key 20 NSArray *allkeys01 = [dic03 allKeys]; 21 // 获取字典中所有的值 22 NSArray *allvalues01 = [dic03 allValues];
② 遍历字典
1 // 一般情况下直接遍历字典 2 NSDictionary *dic04 = [NSDictionary dictionaryWithObjectsAndKeys:@"Jack",@"name",@"male",@"sex",@"21",@"age", nil]; 3 NSLog(@"%@",dic04); 4 5 // 遍历字典 6 for (int i = 0; i < [dic04 count]; i ++) { 7 // 方式一 8 NSString *key_i = [[dic04 allKeys] objectAtIndex:i]; 9 NSString *value_i = [dic04 objectForKey:key_i];// 通过 key 索引数据 10 NSLog(@"%@",value_i); 11 12 //方式二(不推荐) 13 NSString *value_j = [[dic04 allValues] objectAtIndex:i]; 14 NSLog(@"%@",value_j); 15 } 16 17 18 // 数组嵌套字典 19 NSDictionary *stuDic01 = [NSDictionary dictionaryWithObjectsAndKeys:@"Hillary",@"name",@"female",@"sex",@"18",@"age", nil]; 20 NSDictionary *stuDic02 = [NSDictionary dictionaryWithObjectsAndKeys:@"Lee",@"name",@"male",@"sex",@"19",@"age", nil]; 21 NSArray *arr05 = [NSArray arrayWithObjects:stuDic01,stuDic02, nil]; 22 NSLog(@"%@",arr05); 23 // 方式一 24 for (int i =0; i < [arr05 count]; i ++) { 25 26 NSDictionary *dic08_i = [arr05 objectAtIndex:i]; 27 for (int j = 0; j < [dic08_i count]; j ++) { 28 29 NSString *key03_j = [[dic08_i allKeys] objectAtIndex:j]; 30 NSString *value03_j = [dic08_i objectForKey:key03_j]; 31 NSLog(@"%@",value03_j); 32 } 33 } 34 35 // 方式二 36 for(int i = 0; i < [arr05 count]; i ++){ 37 38 NSDictionary *dic09_i = [arr05 objectAtIndex:i]; 39 NSArray *arrayValues02 = [dic09_i allValues]; 40 for (int j = 0; j < [arrayValues02 count]; j ++) { 41 42 NSString *stringValue_j = [arrayValues02 objectAtIndex:j]; 43 NSLog(@"%@",stringValue_j); 44 } 45 } 46 47 48 // 字典嵌套数组 49 NSArray *firstArray05 = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",nil]; 50 NSArray *secondArray05 = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; 51 NSDictionary *bigDic06 = [NSDictionary dictionaryWithObjectsAndKeys:firstArray05,@"first",secondArray05,@"second", nil]; 52 NSLog(@"%@",bigDic06); 53 54 // 方式一 55 for (int i =0; i < [bigDic06 count]; i ++) { 56 NSString *key_i = [[bigDic06 allKeys] objectAtIndex:i]; 57 NSArray *array_i = [bigDic06 objectForKey:key_i]; 58 for (int j =0; j < [array_i count]; j ++) { 59 60 NSString *string_j = [array_i objectAtIndex:j]; 61 NSLog(@"%@",string_j ); 62 } 63 } 64 65 // 方式二 66 NSArray *bigArray01 = [bigDic06 allValues]; 67 for (int i = 0; i <[bigArray01 count]; i ++) { 68 NSArray *smallArray_i = [bigArray01 objectAtIndex:i]; 69 for (int j = 0; j < [smallArray_i count]; j ++) { 70 71 NSString *value_j = [smallArray_i objectAtIndex:j]; 72 NSLog(@"%@",value_j); 73 } 74 } 75 76 77 // 字典嵌套字典 78 NSDictionary *stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Rosie",@"name",@"female",@"sex",@"18",@"age", nil]; 79 NSDictionary *stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Olivia",@"name",@"female",@"sex",@"19",@"age", nil]; 80 NSDictionary *bigDic09 = [NSDictionary dictionaryWithObjectsAndKeys:stuDic1,@"stu1",stuDic2,@"stu2", nil]; 81 // 方式一:采用 for...in... 82 for (NSString *key in bigDic09) { 83 NSDictionary *smallDic = [bigDic09 objectForKey:key]; 84 for (NSString *key in smallDic) { 85 NSString *value = [smallDic objectForKey:key]; 86 NSLog(@"%@",value); 87 } 88 } 89 90 // 方式二 91 for (int i = 0; i < [bigDic09 count]; i ++) { 92 93 NSDictionary *smallDic_i = [bigDic09 objectForKey:[[bigDic09 allKeys] objectAtIndex:i]]; 94 for (int j = 0; j < [smallDic_i count]; j++) { 95 NSString *value = [smallDic_i objectForKey:[[smallDic_i allKeys] objectAtIndex:j]]; 96 NSLog(@"%@",value); 97 } 98 } 99 100 // 方式三 101 NSArray *values06 = [bigDic09 allValues]; 102 for (int i = 0; i < [values06 count]; i ++) { 103 NSDictionary *dic_i = [values06 objectAtIndex:i]; 104 NSArray *array_i = [dic_i allValues]; 105 for (int j = 0; j < [array_i count]; j ++) { 106 107 NSString *str_j = [array_i objectAtIndex:j]; 108 NSLog(@"%@",str_j); 109 } 110 }
NSMutableDictionary
1 - 代码示例
① 如何使用 NSMutableDictionary
1 // 创建 2 NSMutableDictionary *dic06 = [NSMutableDictionary dictionaryWithCapacity:1]; 3 // 可直接追加键值对儿 4 [dic06 setObject:@"Cara"forKey:@"name"]; 5 [dic06 setObject:@"female"forKey:@"sex"]; 6 NSNumber *number = [NSNumber numberWithInt: 18]; 7 [dic06 setObject:number forKey:@"age"]; 8 NSLog(@"%@",dic06); 9 10 // 删除 11 [dic06 removeObjectForKey:@"name"]; 12 NSLog(@"%@",dic06); 13 // 删除所有 14 [dic06 removeAllObjects]; 15 NSLog(@"%@",dic06);
集合
1 - 集合特性:① 存储对象;② 无序存储;③ 随机获取数据;④ 元素不能重复
2 - 代码示例
① NSSet
1 // 存在重复元素 one 2 NSSet *set0 = [[NSSet alloc] initWithObjects:@"one",@"one",@"one",@"two",@"three",@"four", @"a", @"b", @"c", @"d",nil]; 3 NSLog(@"set0 = %@",set0); // 只会显示 1 个 one 4 // set0 = {(one, c, two, d, three, four, a, b)} 5 6 // 存在重复元素 b 7 NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"b", @"d", @"e",set0,nil]; 8 NSLog(@"set1 = %@",set1); 9 // set1 = {(d, b, {(one,c,two,d,three,four,a,b)}, e, a)} 10 11 // 是否存在有相同元素的对象 12 BOOL isIntersect = [set1 intersectsSet:set0]; // 1 13 14 15 // 数组转化成集合 16 NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c",@"d", nil]; 17 NSSet *set3 = [NSSet setWithArray:array]; 18 19 // 集合个数:重复的对象计数为 1 20 NSLog(@"set0 count:%lu", set0.count); // 8 21 // 以数组的形式获取集合中的所有对象 22 NSArray *allObj = [set0 allObjects]; 23 NSLog(@"set0-allObj:%@", allObj); // {(one, c, two, d, three, four, a, b)} 24 25 // 是否包含某个对象 26 NSLog(@"Is set3 coatains a ? %d", [set3 containsObject:@"a"]);// 1 27 // 是否相等 28 NSLog(@"set1 isEqualto set3 ? %d", [set1 isEqualToSet:set3]);// 0 29 // 是否是子集合 30 NSLog(@"set1 isSubSet of set3 ? %d", [set1 isSubsetOfSet:set3]);// 0 31 // 虽然 set1 中存在 set0, 但 set3 也不是 set1 的子集 32 NSLog(@"set3 isSubSet of set1 ? %d", [set3 isSubsetOfSet:set1]);// 0 33 34 NSSet *set4 = [NSSet setWithObjects:@"aaa", @"bbb", nil]; 35 // 加 36 NSSet *set5 = [set4 setByAddingObjectsFromArray:array]; 37 NSLog(@"看看是什么:%@",set5); // {(aaa, c, d, a, bbb, b)} 38 NSSet *set6 = [set5 setByAddingObject:@"fuck"]; 39 NSLog(@"set6 is : %@",set6); // {(aaa, c, d, a, bbb, fuck, b)} 40 41 NSSet *set20 = [NSSet setWithObject:@"i am ok"]; 42 NSSet *set7 = [set6 setByAddingObjectsFromSet:set20]; 43 NSLog(@"set7 is : %@",set7); // {(aaa, c, d, a, bbb, fuck, "i am ok", b)} 44 45 // 任取一个对象 46 NSSet *s = [set7 anyObject]; 47 NSLog(@"%@",s);
② NSMutableSet | NSCountedSet
1 NSMutableSet *mutableSet = [[NSMutableSet alloc] init]; 2 // 添加 3 [mutableSet addObject:@"1"]; 4 [mutableSet addObject:@"11"]; 5 [mutableSet addObject:@"111"]; 6 7 NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3",@"c", nil]; 8 9 // 初始化一个新分配的集合 10 NSMutableSet *mutableSet2 = [[NSMutableSet alloc] initWithCapacity:4]; 11 [mutableSet2 addObject:@"1"]; 12 [mutableSet2 addObject:@"2"]; 13 [mutableSet2 addObject:@"3"]; 14 [mutableSet2 addObject:@"b"]; 15 16 NSMutableSet *mutableSet3 = [NSMutableSet setWithCapacity:3]; 17 [mutableSet3 addObject:@"1"]; 18 [mutableSet3 addObject:@"2"]; 19 [mutableSet3 addObject:@"3"]; 20 21 // 减 22 [mutableSet1 minusSet:mutableSet2]; // 1/2/3/c - 1/2/3/b 23 NSLog(@"minus :%@", mutableSet1); // c 24 [mutableSet3 minusSet:mutableSet2]; // 1/2/3 - 1/2/3/b 25 NSLog(@"minus :%@", mutableSet3); // 空 26 27 // 交集 28 [mutableSet2 intersectSet:mutableSet]; // 1/2/3/b 1/11/111 29 NSLog(@"intersect:%@", mutableSet2); // 1 30 // 合并 31 [mutableSet2 unionSet:mutableSet1]; // 1 c 32 NSLog(@"union:%@", mutableSet2); // {(1, c)} 33 34 // 删除 35 [mutableSet2 removeObject:@"1"]; 36 [mutableSet2 removeAllObjects]; 37 NSLog(@"removeAll:%@", mutableSet2);// 空 38 39 NSLog(@"重新初始化前 mutableSet:%@", mutableSet);// 1, 11, 111 40 mutableSet = [NSMutableSet setWithObjects:@"a",@"b",@"r" ,nil]; 41 NSLog(@"重新初始化后 mutableSet:%@", mutableSet);// a, b, r 42 [mutableSet setSet:mutableSet1]; 43 NSLog(@"%@", mutableSet);// c 44 45 // NSCountedSet 是 NSSet 子类,能够记录集合中元素重复的次数,它是可变集合 46 NSCountedSet *countedSet = [[NSCountedSet alloc] initWithObjects:@"1",@"2",@"3",@"2",@"1", @"1",nil]; 47 NSLog(@"countedSet is :%@,count is: %lu",countedSet,countedSet.count);// 1,2,3 3 48 NSLog(@"%lu",[countedSet countForObject:@"1"]);// 3 49 [countedSet removeObject:@"2"];// 移除了一个 2,还剩下一个 2 50 NSLog(@"countedSet is :%@",countedSet);// 1,2,3 51 [countedSet removeObject:@"2"];// 彻底删干净 2 52 NSLog(@"countedSet is :%@",countedSet);// 1,3 53 54 // 注意同 NSMutableSet 的区别 55 NSMutableSet *mutableSet10 = [NSMutableSet setWithObjects:@"1", @"2",@"2",@"2",@"3",@"c", nil]; 56 [mutableSet10 removeObject:@"2"];// 把 @"2" 这个元素彻底移除,包含重复的 57 NSLog(@"mutableSet10 is %@",mutableSet10);// 1,3,c
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)