Skip navigation.
 
mlCurious about copyWithZone message from NSArrayController in "GC required" project
FROM : Luke Evans
DATE : Sun Jan 13 05:03:47 2008

Hi all,

I have a "gc required" project with some entities in a CoreData model, 
each with requisite subclasses of NSManagedObject.
In particular, I have an entity/class 'A' that declares a property 
returning an NSArray (which actually returns an ordered list of entity/
class 'B' that is defined as a to-many relationship on the entity A).
Now, in my UI, I have a couple of NSArrayControllers.  One of these 
obtains the 'A's from the managed object context, and provides 
instances to UI that in turn uses an NSArrayController bound to the 
property of 'A', such that all the 'B's of a given 'A' are returned in 
the correct order.

Now, all this is now working correctly (as far as I can tell), but 
before I got it to work as expected, I had to fix a problem logged in 
the console as:
*** -[B copyWithZone:]: unrecognized selector sent to instance 0x127f310

The obvious fix of implementing a 'dumb' copyWithZone in the B class 
(that returns B's 'self' ) got past the problem, but it raises a 
number of questions in my Cocoa n00b's mind:
1. Why am I being asked to create a copy of B instances anyway?  I 
would have thought that NSArrayController could work quite happily 
with a reference to my original B's - at least in the GC context. 
Indeed, forcing this to happen with a trivial implementation of 
copyWithZone seems to work happily.
2. Is this something I could change through a configuration in my 
code?  I had a property declaration array of B's returnable from A 
(readonly), and I have tried both retain and assign to see if this 
would affect anything (it didn't).
3. In general, am I _supposed_ to support the NSCopying protocol on my 
classes (even knowing that they will only ever be using in GC) as a 
matter of course.  I can certainly think of scenarios where one might 
like to clone/deepCopy object trees, but had not (yet) read anything 
in the docs that suggests a requirement to do this as a matter of 
course in order to use collections and collection controllers.  I have 
been using instances of these classes in all sorts of collections up 
to now, with no hint of having to implement copyWithZone.

-- Lwe