ターン制の Game Center ゲームに取り組んでいます。既に encodeWithEncoder と initWithEncoder を記述したカスタム クラスをエンコードしようとしています。アンパックしない他の小さなカスタム オブジェクトの 2 つの配列を除いて、これらは正常に動作するようです。私の質問は、これらのクラスのカスタム encodeWithEncoder および initWithEncoder メソッドを少し作成し、それらを NSCoding に準拠させた場合、それらもアンパックされるのでしょうか? または、そのようなエンコーディングをネストすることは許可されていませんか? つまり、配列 f Hands と Plays の配列を持つ Game というオブジェクトがあります。それらに NSCoding を実装した場合、それらも解凍できますか?
編集:要求どおり、機能しない私のコード:
// Implementation of the Player object.
@implementation Player
@synthesize playerID;
@synthesize displayName;
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:playerID forKey:@"PlayerID"];
[coder encodeObject:displayName forKey:@"DisplayName"];
}
- (id)initWithCoder:(NSCoder*)coder
{
self = [super init];
if (self)
{
playerID = [coder decodeObjectForKey:@"PlayerID"];
displayName = [coder decodeObjectForKey:@"DisplayName"];
}
return self;
}
@end
// Implementation of the Play object.
@implementation Play : NSObject
@synthesize player;
@synthesize bCard;
@synthesize playedCards;
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:player forKey:@"Player"];
[coder bCard forKey:@"BCard"];
[coder encodeObject:playedCards forKey:@"PlayedCards"];
}
- (id)initWithCoder:(NSCoder*)coder
{
self = [super init];
if (self)
{
player = [coder decodeObjectForKey:@"Player"];
bCard = [coder decodeObjectForKey:@"BCard"];
playedCards = [coder decodeObjectForKey:@"PlayedCards"];
}
return self;
}
@end
// Implementation of the Hand object.
@implementation Hand : NSObject
@synthesize owner;
@synthesize cards;
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:owner forKey:@"Owner"];
[coder encodeObject:cards forKey:@"Cards"];
}
- (id)initWithCoder:(NSCoder*)coder
{
self = [super init];
if (self)
{
owner = [coder decodeObjectForKey:@"Owner"];
cards = [coder decodeObjectForKey:@"Cards"];
}
return self;
}
@end
// Implementation of the Game object.
@implementation Game : NSObject
@synthesize activePlayer, judge, match, players, play, judgeID, hand, bCard, hands, plays;
// Function to pack up the game object for transmission through Game Center.
- (void) encodeWithCoder:(NSCoder *)coder
{
// Package up all the data required to continue the game.
if([coder allowsKeyedCoding])
{
[coder encodeObject:judge forKey:@"Judge"];
[coder encodeObject:bCard forKey:@"BCard"];
[coder encodeObject:hands forKey:@"Hands"];
[coder encodeObject:plays forKey:@"Plays"];
}
}
// Function to unpack a game object recieved from Game Center.
- (id) initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self)
{
// Decode packed objects.
judge = [coder decodeObjectForKey:@"Judge"];
bCard = [coder decodeObjectForKey:@"BCard"];
hands = [coder decodeObjectForKey:@"Hands"];
plays = [coder decodeObjectForKey:@"Plays"];
// Look for your hand in the hands array.
hand = nil;
for (int i = 0; i < [hands count]; i++)
{
if ([[[(Hand*)hands[i] owner] playerID] isEqualToString:GKLocalPlayer.localPlayer.playerID])
{
hand = hands[i];
break;
}
}
// If your hand was not found, draw a new one.
if (!hand)
{
[self drawHand];
}
}
return self;
}