Switch Case 语句问题
我正在使用 switch case 语句来确定我在游戏中心多人游戏中收到的数据。
我看不出它有什么问题,但是当添加第二个 case 语句时它停止工作。
我已经声明了这一点:
typedef enum
{
kPacketTypeScore,
kPacketTypeReady,
} EPacketTypes;
typedef struct
{
EPacketTypes type;
size_t size;
} SPacketInfo;
typedef struct
{
SPacketInfo packetInfo;
int score;
} SScorePacket;
typedef struct
{
SPacketInfo packetInfo;
bool ready;
} SReadyPacket;
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
// first, assume it's the general SPacketInfo, that way we can access type and size
packet = (SPacketInfo*)[data bytes];
scoreData *scoreDat = [scoreData sharedData];
BOOL rdyReceived;
switch (packet->type)
{
case kPacketTypeScore:
{
SScorePacket* scorePacket = (SScorePacket*)packet;
[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", scoreDat.score, scorePacket->score]];
break;
}
case kPacketTypeReady:
{
SReadyPacket* readyPacket = (SReadyPacket*)packet;
rdyReceived = readyPacket->ready;
if (rdyReceived == FALSE && scoreDat.mpRdy == TRUE) {
[rdyLabel setString:@"Waiting for challenger..."];
}
if (rdyReceived == TRUE && scoreDat.mpRdy == FALSE) {
[rdyLabel setString:@"Challenger is waiting... Ready?"];
}
if (rdyReceived == TRUE && scoreDat.mpRdy == TRUE) {
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[mpView node] withColor:ccWHITE]];
}
break;
}
default:
CCLOG(@"received unknown packet type %i (size: %u)", packet->type, packet->size);
break;
}
}
I am using a switch case statement to determine what data I am receiving in game center multiplayer.
I can't see whats wrong with it but when adding the second case statement it stops working.
I have declared this:
typedef enum
{
kPacketTypeScore,
kPacketTypeReady,
} EPacketTypes;
typedef struct
{
EPacketTypes type;
size_t size;
} SPacketInfo;
typedef struct
{
SPacketInfo packetInfo;
int score;
} SScorePacket;
typedef struct
{
SPacketInfo packetInfo;
bool ready;
} SReadyPacket;
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
// first, assume it's the general SPacketInfo, that way we can access type and size
packet = (SPacketInfo*)[data bytes];
scoreData *scoreDat = [scoreData sharedData];
BOOL rdyReceived;
switch (packet->type)
{
case kPacketTypeScore:
{
SScorePacket* scorePacket = (SScorePacket*)packet;
[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", scoreDat.score, scorePacket->score]];
break;
}
case kPacketTypeReady:
{
SReadyPacket* readyPacket = (SReadyPacket*)packet;
rdyReceived = readyPacket->ready;
if (rdyReceived == FALSE && scoreDat.mpRdy == TRUE) {
[rdyLabel setString:@"Waiting for challenger..."];
}
if (rdyReceived == TRUE && scoreDat.mpRdy == FALSE) {
[rdyLabel setString:@"Challenger is waiting... Ready?"];
}
if (rdyReceived == TRUE && scoreDat.mpRdy == TRUE) {
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[mpView node] withColor:ccWHITE]];
}
break;
}
default:
CCLOG(@"received unknown packet type %i (size: %u)", packet->type, packet->size);
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定
data
是SReadyPacket*
吗?它可能是 nil 或其他类型。
Are you sure that
data
is aSReadyPacket*
?It may be
nil
or some other type.您需要澄清传入的数据到底是什么。您最初将其视为
SPacketInfo
,以获取类型。然后将其转换为其他两个结构之一,其中包含一个SPacketInfo
,但绝对不是SPacketInfo
本身。然后,当您尝试访问所述结构的成员时,您将访问完全未定义的位置。packet
实际上是一个数据包结构,在这种情况下,您应该将开关更改为:或者,它是一个
SPacketInfo
,在这种情况下,您需要获取实际的以其他方式“包”。You need to clarify what exactly the data coming in is. You initially treat it as a
SPacketInfo
, to get the type. Then cast it to one of two other structs, which contain anSPacketInfo
, but are definitely notSPacketInfo
's themselves. Then when you try to access members of said struct, you're accessing completely undefined locations.Either
packet
is actually a packet struct, in which case you should change your switch to:Or, it is a
SPacketInfo
, in which case you'll need to get the actual "packet" in some other way.