Ios 已将无法识别的选择器发送到自定义对象上的实例
我创建了一个自定义类对象操作,其中有三个枚举作为实例变量:Ios 已将无法识别的选择器发送到自定义对象上的实例,ios,xcode,ipad,unrecognized-selector,Ios,Xcode,Ipad,Unrecognized Selector,我创建了一个自定义类对象操作,其中有三个枚举作为实例变量: @interface Action : NSObject <NSCopying> @property ImageSide imageSide; //typedef enum @property EyeSide eyeSide; //typedef enum @property PointType pointType; //typedef enum @property int actionId; - (id) in
@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
在我的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开发新手,对此我感到非常困惑。您声明您的
操作
类符合NSCopying
协议
因此,您需要为该类实现
-(id)copyWithZone:(NSZone*)zone
。在可变字典中用作键的对象必须符合Apple文档中描述的NSCopying
协议(因此必须实现copyWithZone:
)
在本例中,您将对象声明为与NSCopying
协议对应的对象,但未实现该方法。你需要这样做
希望这有帮助
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Action copyWithZone:]: unrecognized selector sent to instance