• 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
Re: NSString property: copy or retain?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString property: copy or retain?


  • Subject: Re: NSString property: copy or retain?
  • From: "Clark Cox" <email@hidden>
  • Date: Mon, 22 Dec 2008 22:01:31 -0500

On Mon, Dec 22, 2008 at 5:41 PM, Ricky Sharp <email@hidden> wrote:
>
> On Dec 22, 2008, at 4:04 PM, Debajit Adhikary wrote:
>
>> Let's say I have a class called SomeClass with a string property name:
>>
>> @interface SomeClass : NSObject{
>>   NSString* name;
>> }
>>
>> @property (nonatomic, retain) NSString* name;
>>
>> @end
>>
>> I understand that name may be assigned a NSMutableString in which case
>> this
>> will may to errant behavior.
>>
>> (1) For strings in general, is it *always* a good idea to use the "copy"
>> attribute instead of "retain"?
>
> No.  This isn't a "one way or another" situation.  It's really up to what
> you need.  Having said that, there are good guidelines as to why you'd want
> to use one over the other.  Look at the memory-management guidelines or
> search the archives.

There is rarely a reason to define an NSString accessor as retain
instead of copy. I would argue that, unless you have an especially
good reason, NSString attributes (as well as NSData, NSDictionary,
NSArray, and any other foundation class that has a mutable counterpart
and implements the NSCopying protocol) should be declared as "copy",
not "retain".

>> (2) Is a "copied" attribute in any way less efficient than such a
>> "retain-ed" attribute?
>
> Definitely.  It will cost more cycles to copy the object vs. retain it.
>  And, will use more memory.  However, the performance and memory costs may
> not have any noticeable impact on your code.  A profiler can tell you for
> sure.

This is not true. For objects like NSString, -copy and -retain are
100% equivalent, and when an NSMutableString instance is passed in,
copy is almost always the right thing to use (as it maintains proper
encapsulation).

--
Clark S. Cox III
email@hidden
_______________________________________________

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

References: 
 >NSString property: copy or retain? (From: "Debajit Adhikary" <email@hidden>)
 >Re: NSString property: copy or retain? (From: Ricky Sharp <email@hidden>)

  • Prev by Date: Re: Core DataTableView Column Sort at launch with bindings?
  • Next by Date: Re: Configuring NSNumberFormatter non-programmatically
  • Previous by thread: Re: NSString property: copy or retain?
  • Next by thread: Re: NSString property: copy or retain?
  • Index(es):
    • Date
    • Thread