How to add to an NSDictionary

前端 未结 6 1347
无人及你
无人及你 2020-12-12 19:00

I was using a NSMutableArray and realized that using a dictionary is a lot simpler for what I am trying to achieve.

I want to save a key as a NSSt

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 19:26

    When ever the array is declared, then only we have to add the key-value's in NSDictionary like

    NSDictionary *normalDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2",@"Value3",@"Key3",nil];
    

    we cannot add or remove the key values in this NSDictionary

    Where as in NSMutableDictionary we can add the objects after intialization of array also by using this method

    NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init];'
    [mutableDict setObject:@"Value1" forKey:@"Key1"];
    [mutableDict setObject:@"Value2" forKey:@"Key2"];
    [mutableDict setObject:@"Value3" forKey:@"Key3"];
    

    for removing the key value we have to use the following code

    [mutableDict removeObject:@"Value1" forKey:@"Key1"];
    

提交回复
热议问题