views:

73

answers:

1

I have a custom class of type NSObject that contains a single NSMutableArray. This class is called Mutable2DArray and is designed to emulate a 2 dimensional array of type NSMutableArray. There is a custom init method - (id)initWithX:(int)x Y:(int)y that asks for the dimensions for the array and allocates the required arrays within the only array the class owns.

My issue is when I try to copy an instance of Mutable2DArray I get an error saying the copyWithZone is an unrecognized selector. I thought copy was a base method of NSObject so I'm confused why I cant create a copy of the instance like this:

    Mutable2DArray *Array1 = [[Mutable2DArray alloc] initWithX:10 Y:10];
Mutable2DArray *Array2 = [Array1 copy];

Am I missing something so obvious here?

A: 

Things that I can think of to check, off the top of my head:

  1. Does the header file actually declare the interface as inheriting from NSObject?
  2. Does your custom initWithX: Y: method call [super init] before finishing?
Endemic
Yes, the header defines the class as inheriting from NSObject and the initialization method does call [super init]
Parad0x13
My apologies, I should have checked the docs first. From the NSObject reference: >NSObject does not itself support the NSCopying protocol. Subclasses must support the protocol and implement the copyWithZone: method. A subclass version of the copyWithZone: method should send the message to super first, to incorporate its implementation, unless the subclass descends directly from NSObject.
Endemic