Objective-C 字典的基本用法(NSDictionary和NSMutableDictionary)
来源:互联网 发布:么么哒 网络词汇 编辑:程序博客网 时间:2024/05/18 01:21
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... //创建字典 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 :%lu", 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;}
NSLog
dic1 :{ key = value;} dic2 :{ key1 = value1; key2 = value2; key3 = value3; key4 = value4;} dic3 :{ key1 = value1; key2 = value2; key3 = value3; key4 = value4;} key3 value :value3 dic count :4 keys :( key2, key3, key1, key4) values :( value2, value3, value1, value4) mutableDic :{ key1 = value1; key2 = value2; key3 = value3; key4 = value4; mkey1 = mvalue1; mkey2 = mvalue2;} set value for key :{ key1 = value1; key2 = value2; key3 = value3; key4 = value4; mkey1 = mvalue1; mkey2 = mvalue2; setKey1 = set1;} set dictionary :{ key1 = value1; key2 = value2; key3 = value3; key4 = value4;} removeForkey :{ key2 = value2; key3 = value3; key4 = value4;} key :key4 value :value4 key :key2 value :value2 key :key3 value :value3 enumerator :value4 enumerator :value2 enumerator :value3 removeObjectsForKeys :{} remove all :{}
0 0
- Objective-C 字典的基本用法(NSDictionary和NSMutableDictionary)
- Objective-C中NSDictionary和NSMutableDictionary的基本用法
- Objective-c 字典对象 (NSDictionary 和 NSMutableDictionary )
- Objective-C 字典:"NSDictionary" 和 "NSMutableDictionary"
- 字典对象(NSDictionary 和 NSMutableDictionary)-Objective-C
- 字典对象(NSDictionary 和 NSMutableDictionary)-Objective-C
- Objective-C语法-字典NSDictionary和可变字典NSMutableDictionary
- Objective-C------NSDictionary、NSMutableDictionary字典对象用法总结
- Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary
- Objective-C学习-字典类型 NSDictionary 和 NSMutableDictionary
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- 群晖服务器如何搭建网站
- (水)POJ-2453 位运算(必看!)
- 1019. General Palindromic Number (20)
- Linux笑我好蠢(1)
- 70. Climbing Stairs
- Objective-C 字典的基本用法(NSDictionary和NSMutableDictionary)
- 广播拦截短信
- spring中scope作用域(转)
- C++连接SQL数据库第一步 系统配置
- leetcode——82——Remove Duplicates from Sorted List II
- Android RecyclerView 使用完全解析 体验艺术般的控件
- 百度地图上自定义添加一个标注
- C++实验4求1000以内所有偶数的和
- 240. Search a 2D Matrix II