2013-03-01 35 views
0

我正在尝试创建一个2玩家Turn Based Match游戏。玩家目前可以轮流使用,但数据实际上并未填充到NSData中。我发现这种方法如何存档和序列化,但我觉得我只是在总体上做错了。这是玩家1结束后执行的代码。目前,我真的只需要保存分数(我这样说是因为我在数据字典中保存了player1id,当我真的不需要时)。更新打开GKTurnBasedMatch NSData

 //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 *数据里没有数据! (注:目前我得到这个错误:)

“类型‘NSUInteger’的集合元素(又名‘无符号在’)不是一个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 whats in this:3042

2013-03-01 15:49:10110 mysite是:3042

2013-03-01 15:49:10.175 myMatchDataDictionary选手1分:0

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

我开始觉得这件事情与[AppDelegate中getGameState] .gameDetail.player1Score

回答

2

更改此

@"Player1score" : myscore, 

@"Player1score" : @(myscore), 

只能添加对象到字典和NSUInteger是不是一个对象,当你通过@(myScore的),你把它变成一个NSNumber对象 - 所以阅读它回来时,你应该做的是:

[myMatchDataDictionary[@"Player1Score"] integerValue]; 
+0

见我上面的编辑。感谢检查出来。这绝对是一步。 – 2013-03-01 20:53:51

+0

明白了。你是一个救生员。 – 2013-03-01 21:42:19