On Mon, Dec 22, 2008 at 5:41 PM, Ricky Sharp <rsh...@mac.com> 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
clarkc...@gmail.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to