0

I'm trying to creating a 2 player Turn Based Match game. The players can currently take turns, but data isn't actually being populated into NSData. I found this method on how to archive and serialize, but I feel like I'm just going about it wrong in general. This is the code that executes after player 1 finishes their turn. Currently, I really only need to save the scores (I say this because I'm saving player1id in the data dict, when I really don't need to).

        //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");

The turn is sent, and myscore does have a score in it, but there is no data in NSData *data! (note: currently I get this error:)

"collection element of type 'NSUInteger' (aka 'unsigned in') is not an Objective-C object"

Tear my code apart and tell me what I'm doing wrong!

EDIT: adding in the output:

    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]);

and the output:

2013-03-01 15:49:10.174 player 1 just took their turn

2013-03-01 15:49:10.174 whats in this: 3042

2013-03-01 15:49:10.175 myscore is: 3042

2013-03-01 15:49:10.175 myMatchDataDictionary player 1 score: 0

2013-03-01 15:49:10.175 Send Turn

I'm starting to think it's something with the [AppDelegate getGameState].gameDetail.player1Score

Edward Glasser
  • 183
  • 4
  • 17

1 Answers1

2

Change this

@"Player1score" : myscore,

into

@"Player1score" : @(myscore),

you can only add objects into a dictionary and NSUInteger is not an object, when you pass @(myScore) you make it into an NSNumber object - so when reading it back you should do it as:

[myMatchDataDictionary[@"Player1Score"] integerValue];
Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41