私はiPhone用のRPGタイプのゲームに取り組んでいます。私がやりたいのは、特定の種類のアイテムだけを特定の量だけ保持できる在庫を持つことです。これを行うには、NSMutableArray を拡張して制限を追加するだけだと思います。私はこれを行うための最良の方法を理解することはできません。これが私の頭の中にあるアイデアです...
バックパック.h
@interface Backpack : NSMutableArray {
Class * arrayClass;
NSMutableArray * array;
int limit;
}
-(id) initWithClass:(Class) type andLimit:(int) num;
@end
バックパック.m
@implementation Backpack
-(id)initWithClass:(Class) type andLimit:(int) num {
arrayClass = type;
limit = num;
array = [NSMutableArray new];
return self;
}
-(void)insertObject:(id) object atIndex:(int) index {
if([object isKindOfClass:arrayClass] && index < limit) {
// Insert it
} else {
// Throw Exception
}
}
@end