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
A mutable dictionary can be changed, i.e. you can add and remove objects. An immutable is fixed once it is created.
create and add:
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
[dict setObject:[NSNumber numberWithInt:42] forKey:@"A cool number"];
and retrieve:
int myNumber = [[dict objectForKey:@"A cool number"] intValue];
For reference, you can also utilize initWithDictionary
to init the NSMutableDictionary
with a literal one:
NSMutableDictionary buttons = [[NSMutableDictionary alloc] initWithDictionary: @{
@"touch": @0,
@"app": @0,
@"back": @0,
@"volup": @0,
@"voldown": @0
}];
By setting you'd use setValue:(id)value forKey:(id)key
method of 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 mutable and "normal" is, well, mutability. I.e. you can alter the contents of NSMutableDictionary
(and NSMutableArray
) while you can't do that with "normal" NSDictionary
and NSArray
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"];
You want to ask is "what is the difference between a mutable and a non-mutable array or dictionary." Many times there different terms are used to describe things that you already know about. In this case, you can replace the term "mutable" with "dynamic." So, a mutuable dictionary or array is one that is "dynamic" and can change at runtime, whereas a non-mutable dictionary or array is one that is "static" and defined in your code and does not change at runtime (in other words, you will not be adding, deleting or possibly sorting the elements.)
As to how it is done, you are asking us to repeat the documentation here. All you need to do is to search in sample code and the Xcode documentation to see exactly how it is done. But the mutable thing threw me too when I was first learning, so I'll give you that one!
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