views:

58

answers:

3

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

I want to save a key as an NSString and a value as an int in the dictionary. firstly , how is this done? Secondly, what is the difference between mutable and a normal dictionary?

Regards.

+1  A: 

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"];

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

Eimantas
Shouldn't you wrap that integer in a NSNumber?
Johan Kool
I have one more issue with this. I need to store a user entered text as string as well as the image reference(the index in the array of images) which the user is saving the text to. I tried a mutableArray of objects that have 2 variables, one for each I just mentioned. that wasnt working well when trying to populate the tableView with the array objects text values. SO I thought of a dictionary, the only problem is it doenst look like I could retrieve data by index, it has to be a key or value like a hashmap right?
alJaree
@Johan - correct and updated!@alJaree - yes, the dictionary is key-value store of sorts. I think in your position you'd be better of with array of dictionaries. Something like `[{image: "/path/to/img/1.jpg", data: "foo bar 1"}, {image: "/path/to/img/2.jpg", data: "foo bar 2"}, ..., {image: "/path/to/img/N.jpg", data: "foo bar N"}]`
Eimantas
This still wont work for me, because I need to retrieve text from each object and use that to fill the favorites UITableView as well as I need an index reference to the image array so that I can retrieve and display the selected favorite image. I tried the object that I described above, but the returned objects when reading from the array where NSStrings for some reason rather than the specific object that was stored. I am not sure what to do here,
alJaree
I am thinking that I might need to just store NSStrings such as @"%d_%@, index, savedTitle and then just split the NSString and take and dipay what I need to. I am thinking in java terms, sure there must be a split function in obj c. :)
alJaree
+3  A: 

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];
Eiko
A: 

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!

Mark Hernandez
Sometimes there isnt a decent enough explanation on the Xcode docs, it gets into deep theory without clear examples, not always, but sometimes. :P Yeah I understand the mutable (able to mutate) part now thanks. :)
alJaree