How to add to NSDictionary

I used NSMutableArray and realized that using a dictionary is much easier for what I'm trying to achieve.

I want to save the key as NSString and the value as int in the dictionary. How it's done? Secondly, what is the difference between a mutable and a normal dictionary?

+73
objective-c iphone nsdictionary
Jul 01 2018-10-10T00:
source share
5 answers

A mutable dictionary can be changed, i.e. You can add and remove objects. Immutable fixed after its creation.

create and add:

 NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10]; [dict setObject:[NSNumber numberWithInt:42] forKey:@"A cool number"]; 

and get:

 int myNumber = [[dict objectForKey:@"A cool number"] intValue]; 
+174
Jul 01 2018-10-10T00:
source share

By setting, you would use the setValue:(id)value forKey:(id)key NSMutableDictionary object:

 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setValue:[NSNumber numberWithInt:5] forKey:@"age"]; 

Or in modern Objective-C:

 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; dict[@"age"] = @5; 

The difference between volatile and "normal" is, well, volatility. That is, you can change the contents of NSMutableDictionary (and NSMutableArray ) until you can do this with the "normal" NSDictionary and NSArray

+29
Jul 01 '10 at 16:47
source share

When an array is declared, then only we need to add the key value in the NSDictionary, for example

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

we cannot add or remove key values ​​in this NSDictionary

Where, as in NSMutableDictionary, we can add objects after array initialization using this method

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

to remove the key value we must use the following code

 [mutableDict removeObject:@"Value1" forKey:@"Key1"]; 
+11
Nov 15 '13 at 10:01
source share

Update version

Objective-c

Create:

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

Edit:

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

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

Swift

Create:

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

Edit:

 dictionary["myKey3"] = 3 
+6
May 24 '14 at 7:19
source share

You want to ask: "What is the difference between a mutable and not mutable array or dictionary." Many times, different terms are used to describe things that you already know about. In this case, you can replace the term “volatile” with “dynamic”. Thus, a mutable dictionary or array is “dynamic” and can change at runtime, while a non-volatile dictionary or array is “static” and defined in your code and not changing at runtime (in other words, you will not add, delete or maybe sort the items.)

Regarding how this is done, you are asking us to repeat the documentation here. All you have to do is search the Xcode sample code and documentation to see how exactly this is done. But the volatile thing left me too when I first found out, so I'll give it to you!

+1
Jul 01 '10 at 16:55
source share



All Articles