Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand your question correctly (given your later comment), you want to convert an immutable copy of an mutable object back to being mutable again.</p> <p>The problem seems to be this:</p> <pre><code>NSMutableString *foo = [NSMutableString stringWithString:@"a mutable object"]; NSMutableDictionary *dictionary1, *dictionary2; dictionary1 = [NSMutableDictionary dictionaryWithObject:foo forKey:@"foo"]; dictionary2 = [[NSMutableDictionary alloc]initWithDictionary:dictionary1 copyItems: YES]; [[dictionary1 objectForKey:@"foo"] appendString:@", mutated"]; [[dictionary2 objectForKey:@"foo"] appendString:@", mutated"]; </code></pre> <p>we can alter the object in dictionary1 just fine, but doing the same to dictionary2 throws an exception.</p> <p>This is because although NSMutableDictionary's initWithDictionary:copyItems: method makes a mutable copy of the dictionary object, it makes immutable copies of its contents.</p> <p>Classes that distinguish between immutable and immutable versions (such as cocoa's basic string, array &amp; dictionary classes) are supposed to implement a copyWithZone: and mutableCopyWithZone: method. Since not all classes implement an mutableCopyWithZone: method, NSMutableDictionary's initWithDictionary:copyItems: method copies each of dictionary1's contents immutably, meaning that dictionary2 contains immutable objects.</p> <p>You can make an mutable copy of an immutable object by sending it a mutableCopy message. But probably a better solution for you would be to add an initWithDictionary:mutableCopyItems: method to NSMutableDictionary with a category:</p> <pre><code>- (id) initWithDictionary:(NSDictionary *)otherDictionary mutableCopyItems:(BOOL)flag { if (flag) { self = [self init]; if (self) for (id key in otherDictionary){ id object = [otherDictionary objectForKey:key]; if ([object respondsToSelector:@selector(mutableCopyWithZone:)]) [self setObject:[object mutableCopy] forKey:key]; else [self setObject:[object copy] forKey:key]; } } else self = [self initWithDictionary:otherDictionary]; return self; } </code></pre> <p>Read these if you want to know the difference between copy, mutableCopy, copyWithZone: and mutableCopyWithZone:</p> <p><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSMutableCopying_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSMutableCopying" rel="nofollow noreferrer">http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSMutableCopying_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSMutableCopying</a></p> <p><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSCopying_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSCopying" rel="nofollow noreferrer">http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSCopying_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSCopying</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload