【问题标题】:Updating Turns in GKTurnBasedMatch with NSData使用 NSData 更新 GKTurnBasedMatch 中的转弯
【发布时间】:2013-03-02 04:53:25
【问题描述】:

我正在尝试创建一个 2 人回合制比赛游戏。玩家目前可以轮流进行,但数据实际上并未填充到 NSData 中。我在如何存档和序列化方面找到了这种方法,但我觉得我通常只是在做错事。这是在玩家 1 完成他们的回合后执行的代码。目前,我真的只需要保存分数(我这样说是因为我将 player1id 保存在数据字典中,而我真的不需要)。

        //changes whose turn it is and sends data.
    NSLog(@"player 1 just took their turn");
    NSUInteger myscore = [AppDelegate getGameState].gameDetail.player1Score;
    NSString *errorStr;
    NSDictionary *myMatchDataDict = @{@"Player1id" : [GKLocalPlayer localPlayer].playerID,
                                   @"Player1score" : myscore,
                                   @"Player2id" : nil,
                                   @"Player2score" : nil };
    NSData *data = [NSPropertyListSerialization dataFromPropertyList:myMatchDataDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorStr];
    GKTurnBasedParticipant *nextParticipant;
    nextParticipant = [currentMatch.participants objectAtIndex:1];
    NSLog(@"game data: %@,", data);
    [currentMatch endTurnWithNextParticipant:nextParticipant matchData:data completionHandler:^(NSError *error) {
        if (error) {
            NSLog(@"%@", error);
        }
    }];
    NSLog(@"Turn Sent");

轮到发送,myscore里面确实有分数,但是NSData *data里面没有数据! (注意:目前我收到此错误:)

“'NSUInteger'(又名'unsigned in')类型的集合元素不是Objective-C对象”

撕开我的代码,告诉我我做错了什么!

编辑: 添加输出:

    NSLog(@"player 1 just took their turn");
    NSUInteger myscore = [AppDelegate getGameState].gameDetail.player1Score;
    NSLog(@"whats in this: %lu", (unsigned long)[AppDelegate getGameState].gameDetail.player1Score);
    NSLog(@"myscore is: %lu", (unsigned long)myscore);
    NSString *errorStr;
    NSDictionary *myMatchDataDict = @{@"Player1id" : [GKLocalPlayer localPlayer].playerID,
                                   @"Player1score" : @(myscore)};
    NSData *data = [NSPropertyListSerialization dataFromPropertyList:myMatchDataDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorStr];
    GKTurnBasedParticipant *nextParticipant;
    nextParticipant = [currentMatch.participants objectAtIndex:1];
    NSLog(@"myMatchDataDictionary player 1 score: %ld,", (long)[myMatchDataDict[@"Player1Score"] integerValue]);

和输出:

2013-03-01 15:49:10.174 玩家 1 轮到他们了

2013-03-01 15:49:10.174 这是什么:3042

2013-03-01 15:49:10.175 我的分数是:3042

2013-03-01 15:49:10.175 myMatchDataDictionary 玩家 1 得分:0

2013-03-01 15:49:10.175 发送转

我开始认为这与 [AppDelegate getGameState].gameDetail.player1Score 有关

【问题讨论】:

    标签: iphone ios xcode gkturnbasedmatch


    【解决方案1】:

    改变这个

    @"Player1score" : myscore,
    

    进入

    @"Player1score" : @(myscore),
    

    您只能将对象添加到字典中,而 NSUInteger 不是对象,当您将 @(myScore) 传递给一个 NSNumber 对象时,您应该将其读取为:

    [myMatchDataDictionary[@"Player1Score"] integerValue];
    

    【讨论】:

    • 见我上面的编辑。感谢检查出来。这绝对是一个步骤。
    • 知道了。你是救生员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    相关资源
    最近更新 更多