• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Use of assign vs. copy for accessors in Garbage Collected app
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Use of assign vs. copy for accessors in Garbage Collected app


  • Subject: Use of assign vs. copy for accessors in Garbage Collected app
  • From: Rick Hoge <email@hidden>
  • Date: Tue, 27 Nov 2007 22:42:14 -0500


Hi -

I'm experimenting with use of the @property declaration in a new app. Quite a few of my classes would be substantially simplified if this works as advertised. I'm also evaluating whether this new application would be a good candidate for Garbage Collection.

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? 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?


I noticed that you get the compiler warning if you don't include any attribute, and "assign" is used by default in a GC app. If you explicitly specify (assign), there is no warning.

I need to know whether there may be any hidden problems in using @property(assign) in a GC app in cases where I don't want to copy the object which is input as an instance variable. Maybe I have missed something about class design...

Thanks for any comments,

Rick






_______________________________________________

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


  • Follow-Ups:
    • Re: Use of assign vs. copy for accessors in Garbage Collected app
      • From: Shawn Erickson <email@hidden>
  • Prev by Date: Re: Creating a color image with NSBitmapImageRep and greyscale data
  • Next by Date: Re: Use of assign vs. copy for accessors in Garbage Collected app
  • Previous by thread: NSError and proper cleanup
  • Next by thread: Re: Use of assign vs. copy for accessors in Garbage Collected app
  • Index(es):
    • Date
    • Thread