-
NSDictionary使用小结(字典)
- #import <Foundation/Foundation.h>
-
-
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
-
- //创建字典
- NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
- NSLog(@"dic1 :%@", dic1);
-
-
- //创建多个字典
- NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
- @"value1", @"key1",
- @"value2", @"key2",
- @"value3", @"key3",
- @"value4", @"key4",
- nil];
- NSLog(@"dic2 :%@", dic2);
-
-
- //根据现有的字典创建字典
- NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
- NSLog(@"dic3 :%@", dic3);
-
-
- //根据key获取value
- NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);
-
- //获取字典数量
- NSLog(@"dic count :%d", dic3.count);
-
- //所有的键集合
- NSArray *keys = [dic3 allKeys];
- NSLog(@"keys :%@", keys);
-
- //所有值集合
- NSArray *values = [dic3 allValues];
- NSLog(@"values :%@", values);
-
-
-
- NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"mvalue1", @"mkey1",
- @"mvalue2", @"mkey2", nil];
- //添加现有的字典数据
- [mutableDic addEntriesFromDictionary:dic3];
- NSLog(@"mutableDic :%@",mutableDic);
-
- //添加新的键值对象
- [mutableDic setValue:@"set1" forKey:@"setKey1"];
- NSLog(@"set value for key :%@",mutableDic);
-
- //以新的字典数据覆盖旧的字典数据
- [mutableDic setDictionary:dic2];
- NSLog(@" set dictionary :%@",mutableDic);
-
- //根据key删除value
- [mutableDic removeObjectForKey:@"key1"];
- NSLog(@"removeForkey :%@",mutableDic);
-
- //快速遍历
- for(id key in mutableDic) {
- NSLog(@"key :%@ value :%@", key, [mutableDic objectForKey:key]);
- }
-
- //枚举遍历
- NSEnumerator *enumerator = [mutableDic keyEnumerator];
- id key = [enumerator nextObject];
- while (key) {
- NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);
- key = [enumerator nextObject];
- }
-
-
- //根据key数组删除元素
- [mutableDic removeObjectsForKeys:keys];
- NSLog(@"removeObjectsForKeys :%@",mutableDic);
-
- [mutableDic removeAllObjects];
- //删除所有元素
- NSLog(@"remove all :%@", mutableDic);
- }
- return 0;
- }
-
相关阅读:
Make something people want
POJ 3662 Telephone Lines (二分+Dijkstra)
POJ 2010 Moo University
HDU 6627 2017ACM/ICPC亚洲区域赛沈阳站 Rabbits
HDU 6625 2017ACM/ICPC亚洲区域赛沈阳站 Little Boxes(高精度整数加法)
Codeforces 877D Olya and Energy Drinks(BFS+剪枝)
CodeForces 876B Divisiblity of Differences (数学水题)
POJ 2139 Six Degrees of Cowvin Bacon (Floyd最短路)
UESTC 1057 秋实大哥与花 (线段树)
Linux安装Oh-my-zsh
-
原文地址:https://www.cnblogs.com/as5346/p/4475353.html
Copyright © 2020-2023
润新知