0

我正在尝试创建一个 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 有关

4

1 回答 1

2

改变这个

@"Player1score" : myscore,

进入

@"Player1score" : @(myscore),

您只能将对象添加到字典中,而 NSUInteger 不是对象,当您传递 @(myScore) 时,您会将其变为 NSNumber 对象 - 所以在读回它时,您应该这样做:

[myMatchDataDictionary[@"Player1Score"] integerValue];
于 2013-03-01T20:40:54.957 回答