NSDictionary不可变字典的使用
字典:
key ----> value
索引 ----> 文字内容
里面存储的东西都是键值对
- // 创建字典
- NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];
- // 创建字典
- NSArray *keys = @[@"name", @"address"];
- NSArray *objects = @[@"jack", @"北京"];
- NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
- /*
- NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
- @"jack", @"name",
- @"北京", @"address",
- @"32423434", @"qq", nil];
- */
-
- // 创建字典
- NSDictionary *dict3 = @{@"name" : @"jack", @"address" : @"北京"};
-
- //访问元素
- id obj = [dict3 objectForKey:@"name"];
-
- // 访问元素
- id obj = dict[@"name"];
复制代码
NSMutableDictionary可变字典的使用
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
-
- // 添加键值对
- [dict setObject:@"jack" forKey:@"name"];
-
-
- [dict setObject:@"北京" forKey:@"address"];
-
- [dict setObject:@"rose" forKey:@"name"];
-
-
- // 移除键值对
- [dict removeObjectForKey:<#(id)#>];
-
- // 访问元素
- NSString *str = dict[@"name"];
复制代码 |
|