您的当前位置:首页object-c 之NSMutableDictionary

object-c 之NSMutableDictionary

2024-12-11 来源:哗拓教育

//可变字典 NSMutableDictionary

1 创建空的字典对象  初始化方法
        NSMutableDictionary * mulDict = [NSMutableDictionary dictionary];
2 开辟固定容量大小的可变字典对象
        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:20];
3 用传入字典对象构造字典对象
        NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithDictionary:@{@"one":@"1"}];
        NSLog(@"dict1 = %@",dict1);
 4 修改key对应的值 或者 添加键值对
        [dict1 setObject:@"2" forKey:@"two"];
          NSLog(@"dict1 = %@",dict1);
5 删除某个key值对应的键值对
        [dict1 removeObjectForKey:@"two"];
          NSLog(@"dict1 = %@",dict1);
6 删除key数组对应的键值对
  [dict1 removeObjectsForKeys:NSArray];
7 添加传入字典的键值对 到字典中
        [dict1 addEntriesFromDictionary:@{@"one":@"2"}];
        NSLog(@"dict1 = %@",dict1);
8 删除所有键值对
        [dict1 removeAllObjects];
9 重置或者修改字典
        [dict1 setDictionary:@{@"one":@"1"}];
    /**
    { 字符串1 :数组1 [{字典1},{},{}]
      字符串2 :数组2
      字符串3 :数组3
     }
     */
显示全文