MobileCushion
MobileCushion

Reputation: 7095

Unrecognized selector sent to instance on custom object

I created a custom class object Action with three enums as instance variables:

@interface Action : NSObject  <NSCopying>

@property ImageSide imageSide; //typedef enum 
@property EyeSide eyeSide; //typedef enum 
@property PointType pointType;  //typedef enum
@property int actionId;

- (id) initWithType:(int)num eyeSide:(EyeSide)eyes type:(PointType)type imageSide:(ImageSide)image;

@end

With this implementation:

@implementation Action
@synthesize imageSide;
@synthesize eyeSide;
@synthesize pointType;
@synthesize actionId;

- (id) initWithType:(int)num eyeSide:(EyeSide)eyes type:(PointType)type imageSide:(ImageSide)image {    
    // Call superclass's initializer
    self = [super init];
    if( !self ) return nil;

    actionId = num;
    imageSide = image;
    eyeSide = eyes;
    pointType = type;

    return self;
}
@end

And in my ViewController, I try to add it as a key to a NSMutableDictionary object, like this:

   Action* currentAction = [[Action alloc] initWithType:0 eyeSide:right type:eye imageSide:top];
    pointsDict = [[NSMutableDictionary alloc] initWithCapacity:20];
    [pointsDict setObject:[NSValue valueWithCGPoint:CGPointMake(touchCoordinates.x, touchCoordinates.y)] forKey:currentAction];

However, I get this error, when setObject is called:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Action copyWithZone:]: unrecognized selector sent to instance 

I went through some answers in SO related to this error, but no one seems to solve this problem, and as I am new to iOS development I'm quite puzzled by this.

Upvotes: 1

Views: 1461

Answers (2)

iGranDav
iGranDav

Reputation: 2460

The Object you use as a a key in your mutable dictionnary must conform the NSCopying protocol (So copyWithZone: must be implemented) as decribed in the Apple documentation here

In your case, you declare the object as corresponding the the NSCopying protocol but you don't implement the method. You need to.

Hope this helps

Upvotes: 1

Eiko
Eiko

Reputation: 25632

You declare your Action class to conform to the NSCopying protocol.

So you need to implement -(id)copyWithZone:(NSZone *)zone for that class.

Upvotes: 1

Related Questions