Re: Use of assign vs. copy for accessors in Garbage Collected app
Re: Use of assign vs. copy for accessors in Garbage Collected app
- Subject: Re: Use of assign vs. copy for accessors in Garbage Collected app
- From: Greg Parker <email@hidden>
- Date: Wed, 28 Nov 2007 00:05:00 -0800
Rick Hoge wrote:
In reading the docs, a question came up regarding whether to use
@property(copy) or @property(assign). In particular the following
text is relevant:
assign
Specifies that simple assignment should be used in the setter. This
is the default.
If you use this keyword and your application does not use garbage
collection (GC), you get a compiler warning as this is rarely the
correct behavior. For non-GC applications, you must explicitly
specify one of the storage behavior attributes to avoid a warning
for objects.
For applications that use GC, you will get a warning for this
attribute if the variable adopts the NSCopying protocol as assign
would typically be incorrect in that situation.
Can someone spell out for me why assign is bad in a GC app, and copy
is good?
That documentation is misleading. (Please file a bug report!) The
intent is more like this:
* assign uses simple assignment in the getter
* assign is the default
* With GC off, you get a warning if you specify none of assign, copy,
or retain. (This encourages you think about what memory management you
want and type it explicitly, because the difference between `assign`
and `retain` is large and you'll crash or leak if it's wrong.)
* With GC on, you don't get a warning if you use the default, unless
the property's type is a class that conforms to NSCopying. (The
default is usually what you want; with GC the biggest difference
between assign and retain is that retain is slower.)
The upshot is: `assign` is usually what you want with GC, and if you
set "Objective-C Garbage Collection = Required" in Xcode then you
won't get the warning. "Rarely correct" is too strong for the non-GC
case, and almost backwards for the GC case.
I would expect this would depend on the nature of the relationship
between the objects being represented. I can see how (copy) would
make sense in many cases if the value being assigned was an NSString
or some small and transient object. However I also have cases where
object A has an instance variable which is used to refer to another
large, long-lived object B which should not be copied but to which a
reference is necessary in object A. In previous versions I would
have just used assignment with the appropriate retain on the new
object and release on the old. Is this now wrong?
You're right. Most properties use `assign` in GC code. You should only
use `copy` if you actually want a copy (for example, you want
protection against later mutation of the object).
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden