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?
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.
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.
2 - mutableCopy always creates a mutable copy means you can modify the object.
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With