> On May 18, 2015, at 3:48 PM, Jens Alfke <[email protected]> wrote:
> 
> My colleague just ran into an issue with a synthesized property of type 
> NSMutableDictionary*, where after he assigned a value to the property, the 
> property’s value was no longer mutable:
> 
>       @interface Foo
>       @property (copy) NSMutableDictionary* moot;
>       @end
> 
>       …
>       // foo is an instance of Foo
>       foo.moot = [NSMutableDictionary dictionaryWithObject: @“hi” forKey: 
> @“greeting”];
>       foo.moot[@“greeting”] = @“bye”;  // EXCEPTION RAISED
> 
> The exception occurs because the value of foo.moot is now a non-mutable 
> NSDictionary.
> 
> The culprit seems to be the ‘copy’ modifier in the property declaration. The 
> synthesized setter method is using -copy instead of -mutableCopy, so the 
> object stored into the instance variable is no longer a mutable dictionary.
> 
> So … is this a bug in the compiler, or just a weird edge case one needs to be 
> aware of when using properties of mutable types?

The behavior is deliberate. A synthesized (copy) property always sends the 
-copy message. If that isn't what you want then you shouldn't use a (copy) 
property.

It has been suggested in the past to add a (mutableCopy) property. If you have 
a compelling use case for such a thing you should file a bug report.

It's probably feasible for the compiler to warn if you declare a (copy) 
property of a type that conforms to NSMutableCopying, on the assumption that 
-copy returns an incompatible immutable copy. You should file a bug about that 
too.


-- 
Greg Parker     [email protected]     Runtime Wrangler



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to