nsdictionary copy vs mutable copy

Jacksonkr picture Jacksonkr · Apr 22, 2012 · Viewed 10.9k times · Source

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?

Answer

Williham Totland picture Williham Totland · Apr 22, 2012

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.