我有一个typedef结构
typedef struct {
Message message;
uint32_t randomNumber;
} MessageRandomNumber;
我有这个代码将随机数发送给GKMatch中的所有其他玩家:
-(void)sendRandomNumber {
MessageRandomNumber message;
message.message.messageType = kMessageTypeRandomNumber;
message.randomNumber = _ourRandomNumber;
NSLog(@"My random number is %d", _ourRandomNumber);
NSData * data = [NSData dataWithBytes:&message length:sizeof(MessageRandomNumber)];
NSLog(@"Sent my random number to the other players");
[MultiplayerNetworking sendData:data];
}
+(void)sendData:(NSData *)data {
NSError * error;
GCTurnBasedMatchHelper * gamekitHelper = [GCTurnBasedMatchHelper sharedGameKitHelper];
BOOL success = [gamekitHelper.match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
NSLog(@"Sent data to all players %s ", success ? "Successfully" : "has failed");
gamekitHelper = nil;
if (!success) {
NSLog(@"Error sending data:%@", error.localizedDescription);
[[GCTurnBasedMatchHelper sharedGameKitHelper] appAlert:@"Error Sending Data" alertMessage:[NSString stringWithFormat:@"%@", error.localizedDescription] viewController:[DominoTableViewController dominotableViewController] gkMatchDisconnect:true];
}
}
我有以下代码来接收GKMatch中所有其他玩家的随机数:
-(void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
Message * message = (Message *)[data bytes];
if (message->messageType == kMessageTypeRandomNumber) {
MessageRandomNumber * messageRandomNumber = (MessageRandomNumber *) [data bytes];
NSLog(@"Received random number: %d", messageRandomNumber- >randomNumber);
我的问题是,在我的iPhone 5(无SIM卡,iOS 9.3.1)和iPod Touch(第5代)上,messageRandomNumber-> randomNumber在收到其他3名玩家的随机数时总是等于0在比赛中连接。这个完全相同的代码在iphone 5s上运行得很好(没有SIM卡,iOS 9.3.1)。这种行为有解释吗?