2013-03-01 36 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