0

我正在开发一款基于回合制的 Game Center 游戏。我正在尝试编码一个自定义类,我已经为其编写了 encodeWithEncoder 和 initWithEncoder。这些似乎工作正常,除了两个不解包的其他小自定义对象数组。我的问题是,如果我为这些类编写少量自定义 encodeWithEncoder 和 initWithEncoder 方法,并使它们符合 NSCoding,它们也会被解包吗?或者你不允许嵌套这样的编码?即,我有一个名为 Game 的对象,其中有一个数组 f Hands 和一个 Plays 数组。如果我要为他们实现 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;
}
4

0 回答 0