Re: NSManagedObject, NSString property retain vs copy
Re: NSManagedObject, NSString property retain vs copy
- Subject: Re: NSManagedObject, NSString property retain vs copy
- From: Jean-Daniel Dupas <email@hidden>
- Date: Fri, 31 Jul 2015 12:15:10 +0200
> Le 30 juil. 2015 à 18:26, Fritz Anderson <email@hidden> a écrit :
>
> On 30 Jul 2015, at 11:03 AM, Trygve Inda <email@hidden> wrote:
>
>> It seems Apple is using retain rather than copy for NSString properties in
>> an NSManagedObject subclass.
>>
>> I was always under the impression that copy should be used for NSString, so
>> why the retain??
>
> For an immutable string, -copy is implemented as a -retain. -copy is a guard against the receiver’s relying on the unchanging contents of a string whose contents can be changed. If the contents cannot in fact be changed, there’s no point in allocating new memory and copying the bytes into it.
>
> It’s an implementation detail; what makes you believe it makes a difference?
If it is in the property declaration, it is not an implementation detail, it is part of the public API.
And as the receiver can’t guarantee that a passed NSString is not a NSMutableString under the hood, it should always declare property as copy, so the fact that some sample code (I guess this is what the op is talking about) use retain is dubious.
_______________________________________________
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