NSMutableDictionary的基本使用 - OC

  • 可变字典的创建
    //1)可变字典的创建
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; //创建空字典
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:3];


  • [dict1 setValue:@"zhaosi" forKey:@"ls"];//因为key值重复了,所以添加不上
    [dict1 setValue:@"lisi" forKey:@"ls"]; // ls

      [dict1 setValue:@"liuneng" forKey:@"ln"];
      NSLog(@"%@",dict1);
    

  • [dict1 removeObjectForKey:@"ls"];
    //全部删除
    // [dict1 removeAllObjects];


  • [dict1 setObject:@"zhaosi" forKey:@"ls"];
    //简写形式
    dict1[@"ls"] = @"xxxxx";
    NSLog(@"%@",dict1);


  • //获取所有的key值
    NSArray *arr = [dict1 allKeys];
    if([arr containsObject:@"ls"]){

          NSLog(@"存在ls的key");
      }

你可能感兴趣的:(NSMutableDictionary的基本使用 - OC)