2012-10-11 41 views
0

我創建了一個自定義的類的對象操作有三個枚舉實例變量:無法識別的選擇發送到實例上的自定義對象

@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]; 

但是,我得到t他的錯誤,當的setObject叫做:

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

我通過SO與這個錯誤一些答案去了,但似乎沒有人來解決這個問題,並作爲我新的iOS開發我的挺納悶這個。

回答

1

您聲明Action類符合NSCopying協議。

因此,您需要爲該類實施-(id)copyWithZone:(NSZone *)zone

+0

確實。非常感謝。 – MobileCushion

1

你在你的可變dictionnary AA密鑰必須符合的NSCopying協議使用對象(所以copyWithZone:必須執行)作爲蘋果文檔here

在你的情況在描述下,可以聲明對象爲相應的NSCopying協議,但您不實施該方法。你需要。

希望這會有幫助

相關問題