Skip to content

Object.clone() support

Tom Ball edited this page Feb 26, 2014 · 3 revisions

The Foundation framework has a NSCopying protocol that is similar to Object.clone(), but different enough to be incompatible. It's a useful protocol that used widely, however, so Cloneable classes should implement it.

The solution was to add clone() to NSObject using a category, which does the same shallow copy that Object.clone() does. The Cloneable interface is mapped to NSCopying, and a copyWithZone: method is added to Cloneable classes that calls the class's clone() method.

One issue with this solution is that because properties are used for instance variables, there is a memory leak when a clone() method updates any fields after calling super.clone(). The solution is to have the statement generator modify assignment expressions in a clone() method to directly set its fields.

We've Moved!

The J2ObjC wiki has moved to j2objc.org.

Clone this wiki locally