1

インスタンス変数として3つの列挙型を使用してカスタムクラスオブジェクト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開発に慣れていないので、これにはかなり戸惑っています。

4

2 に答える 2

1

プロトコルActionに準拠するようにクラスを宣言します。NSCopying

-(id)copyWithZone:(NSZone *)zoneしたがって、そのクラスに実装する必要があります。

于 2012-10-11T13:44:48.050 に答える
1

変更可能な辞書でキーとして使用するオブジェクトは、Apple のドキュメントで説明されているように、NSCopyingプロトコルに準拠する必要があります (実装する必要があります)。copyWithZone:

あなたの場合、オブジェクトをNSCopyingプロトコルに対応するものとして宣言しますが、メソッドを実装しません。必要がある。

お役に立てれば

于 2012-10-11T13:46:09.493 に答える