- 3つの「モノ」を単一のインデックス位置にリンクする一般的な方法は、モノを単一のクラスに配置してから、そのクラスのオブジェクトをインデックス位置に配置することです。
- デッキは、objective-cの編集可能なオブジェクトのコンテナである単純なNSMutableArrayにすることができます。
- 選択は2番目のNSMutableArrayにすることも、選択したプロパティを各カードに追加することもできます。どちらも実行可能な選択肢ですが、アルゴリズムは状況によって異なります。
したがって、静的な背面画像を含むCardClassを用意します(つまり、背面画像は、インスタンス化するすべてのオブジェクトに存在します)。正面の画像と説明のプロパティをクラスに追加します。次に、これらのオブジェクトのコレクションとしてリストを作成します。
//Card.h
@interface Card : NSObject
{
UIImage * back;
UIImage * front;
NSString * description;
}
@property (readonly) UIImage * back;
@property (readonly) UIImage * front;
@property (readonly) NSString * description;
- (id) initWithFront:(UIImage *)setFront Description:(NSString*)setDescription;
@end
//Card.m
#import "Card.h"
static UIImage * backimage = nil;
@implementation Card
@synthesize back;
@synthesize front;
@synthesize description;
+(void) initialize
{
if (!backimage){
backimage = [[UIImage alloc]initWithContentsOfFile:@"imagefile.png"]; //though imagefile.png will be replaced with a reference to a plist.info string
}
}
- (id) initWithFront:(UIImage *)setFront Description:(NSString*)setDescription{
if (self = [super init]){
front=setFront;
description= setDescription;
back = backimage;
}
return self;
}
@end
//... elsewhere, perhaps your main viewDidLoad method
NSMutableArray *deck = [NSMutableArray initWithCapacity:36];
Card * card1 = [[CardClass alloc] initWithFront:@"card1.png" Description:@"card 1"];
[deck addObject:card1];
... //etc to create the remaining cards in the whole deck
NSMutableClassを拡張して、シャッフルルーチンを作成します。NSMutableArrayをシャッフルするための最良の方法は何ですか?を参照してください。