Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "-copy" and "-copyWithZone:"?

I know that copy creates an immutable copy of an object but i just want to know how copywithzone works and what is the basic difference between copy and copywithzone

like image 905
prakhar Avatar asked Aug 21 '12 05:08

prakhar


People also ask

What is copyWithZone?

Returns a new instance that's a copy of the receiver.

What is NSCopying?

The copy() method comes from NSCopying -- a protocol built to do exactly what it implies: giving objects the ability to generate copies of themselves.

How do I copy an object in Objective C?

<NSCopying> Protocol and copyWithZone Method Implementation Now when executed, the above code creates a copy of the object referenced by account1 and assigns a pointer to the new object to variable account2.


1 Answers

copy is just short for copyWithZone:, using the default zone.

it's rare that you would call copyWithZone: directly, although defining/implementing it is required in order to adopt @protocol NSCopying. so you would normally see copyWithZone: only within an implementation of copyWithZone:. similarly, you would typically avoid implementing copy, and just let the default implementation of copy call through copyWithZone:.

like image 135
justin Avatar answered Oct 02 '22 15:10

justin