0

2 プレーヤーのターン ベースのマッチ ゲームを作成しようとしています。現在、プレーヤーは順番にプレイできますが、データは実際には NSData に入力されていません。アーカイブとシリアル化の方法についてこの方法を見つけましたが、一般的に間違っているように感じます。これは、プレイヤー 1 がターンを終了した後に実行されるコードです。現在、私は本当にスコアを保存する必要があるだけです (これは、本当に必要がないときに、データ dict に 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 myscore は: 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 に答える