How to add to an NSDictionary

前端 未结 6 1330
无人及你
无人及你 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:32

    Update version

    Objective-C

    Create:

    NSDictionary *dictionary = @{@"myKey1": @7, @"myKey2": @5}; 
    

    Change:

    NSMutableDictionary *mutableDictionary = [dictionary mutableCopy];     //Make the dictionary mutable to change/add
    mutableDictionary[@"myKey3"] = @3;
    

    The short-hand syntax is called Objective-C Literals.

    Swift

    Create:

    var dictionary = ["myKey1": 7, "myKey2": 5]
    

    Change:

    dictionary["myKey3"] = 3
    

提交回复
热议问题