> On Jan 22, 2016, at 06:14, Dave <[email protected]> wrote:
> 
> 
>> On 21 Jan 2016, at 23:40, Quincey Morris 
>> <[email protected]> wrote:
>> 
>> On Jan 21, 2016, at 15:22 , Dave <[email protected] 
>> <mailto:[email protected]>> wrote:
>>> 
>>> I’m relying of the copy attribute for the NSString’s, do I need to change 
>>> these to do a [xxxxxxx copy] too
>> 
>> If you’re writing the setter yourself, you must do the copy yourself. If 
>> you’re using the synthesized setter, it’s done for you.
>> 
>> In your own code, you may as well be liberal with ‘copy’. It’s basically 
>> free (in run-time cost) in situations where you don’t need it. You don’t 
>> save anything by leaving it out.
>> 
> 
> I’ve always been confused over what *actually* happens when you do something 
> like this:
> 
> @property (copy)      NSString*       pString;
> 
> 
> self.pString = [anotherString copy];
> 
> Do two new NSString objects get created? (I mean using the synthesized setter)

The -copy method is called twice, but what that means in practical terms 
depends on the object being copied.
If you're talking about NSString specifically, zero copies get created, as 
-copy on immutable foundation types is equivalent to -retain (or is a no-op 
under ARC).

If "anotherString" is an NSMutableString, then one copy will be created by the 
explicit call to -copy in your example. But that copy will, itself, be an 
immutable NSString, so the second copy will be a no-op.

And, of course, if we were talking about some other class that doesn't have a 
similar optimization, two copies will be created.

Regardless of the circumstances, that explicit copy is redundant (but harmless) 
under ARC, and will cause a memory leak under MRR.
_______________________________________________

Cocoa-dev mailing list ([email protected])

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

This email sent to [email protected]

Reply via email to