Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nsdictionary copy vs mutable copy

I have

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
id dict = [myDictionary copy];

but is dict now just a regular NSDictionary? Or is it a copy of the NSMutableDictionary?

Also, is there any way to go from mutable to non-mutable?

like image 877
Jacksonkr Avatar asked Apr 22 '12 17:04

Jacksonkr


People also ask

What is the difference between NSDictionary and NSMutableDictionary?

The key difference: NSMutableDictionary can be modified in place, NSDictionary cannot. This is true for all the other NSMutable* classes in Cocoa. NSMutableDictionary is a subclass of NSDictionary, so everything you can do with NSDictionary you can do with both.

What is the difference between NSMapTable vs NSDictionary?

NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.

What is a mutable copy?

2 - mutableCopy always creates a mutable copy means you can modify the object.

What is NSDictionary?

An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.


1 Answers

There are two methods involved here; -copy and -mutableCopy.

If the class holds a distinction; -copy always creates an immutable copy; and -mutableCopy always creates a mutable copy.

If the class holds no distinction; -copy always creates a true copy.

So yes, dict is now an NSDictionary, containing the objects in the dictionary.

like image 197
Williham Totland Avatar answered Oct 04 '22 19:10

Williham Totland