你能嵌套数据编码吗?

时间:2013-06-09 03:27:25

标签: ios objective-c nsdata nscoding

我正在开发一款回合制游戏中心游戏。我正在尝试编码一个自定义类,我已经编写了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;
}

0 个答案:

没有答案
相关问题