I am working on a turn-based Game Center game. I am trying to encode a custom class for which I have already written encodeWithEncoder and initWithEncoder. Those seem to work fine, except for two arrays of other little custom objects that don't unpack. My question is, if I write little custom encodeWithEncoder and initWithEncoder methods for those classes, and make them conform to NSCoding, will they be unpacked as well? Or are you not allowed to nest encodings like that? i.e., I have an object called a Game which has an array f Hands and an array of Plays among other things. If I were to implement NSCoding for them, would I be able to unpack them as well?
EDIT: As requested, my code that isn't working:
// 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;
}