我用三个枚举作为实例变量创建了一个自定义类对象Action:
@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
通过此实现:
@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
在我的ViewController中,我尝试将其作为键添加到NSMutableDictionary对象,如下所示:
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];
但是,调用setObject时出现此错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Action copyWithZone:]: unrecognized selector sent to instance
我在SO中找到了与此错误相关的一些答案,但是似乎没人能解决这个问题,由于我是iOS开发的新手,对此我感到很困惑。
最佳答案
您声明自己的Action
类符合NSCopying
协议。
因此,您需要为该类实现-(id)copyWithZone:(NSZone *)zone
。