问题描述
我想通过 Gamecenter 将 NSString 从另一台 iPhone/iPad 发送到另一台 iPhone/iPad但它会因 EXC_BAD_ACCESS 而崩溃
I want to send NSString form another one to another one iPhone/iPad via Gamecenter but it crash with EXC_BAD_ACCESS
在 .h 文件中
typedef enum {
kMessageTypeRandomNumber = 0,
kMessageTypeGameBegin,
kMessageTypeSubmit,
kMessageTypeExchange,
kMessageTypePickup,
kMessageTypePass,
kMessageTypeGameOver
} MessageType;
typedef struct {
MessageType messageType;
} Message;
typedef struct {
Message message;
NSString *submitTile;
} MessageSubmit;
在 .m 文件中
- (void)sendData:(NSData *)data {
NSError *error;
BOOL success = [[GCHelper sharedInstance].match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
if (!success) {
CCLOG(@"Error sending init packet");
[self matchEnded];
}
}
-(void)sendSubmit:(NSString *) submitTile {
MessageSubmit message;
message.message.messageType = kMessageTypeSubmit;
message.submitTile = submitTile;
NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageSubmit)];
[self sendData:data];
}
如果我点击 CCMenu 图像,它将调用 onSubmit 函数这里是 onSubmit 函数
and the if I click on CCMenu image it will call onSubmit function and here are onSubmit function
-(void)onSubmit
{
NSString *submitStr = @"1-7-7 =-7-8 1-7-9";
[self sendSubmit:submitStr];
}
最后一个是 didReceiveData 方法
and the last one is didReceiveData method
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
if (message->messageType == kMessageTypeSubmit) {
MessageSubmit * messageSubmit = (MessageSubmit *) [data bytes];
NSString *submitStr = messageSubmit->submitTile;
NSLog(@"SubTile %@",submitStr);
}
}
它在 NSString *submitStr = messageSubmit->submitTile;
行上有 EXC_BAD_ACCESS.
it have EXC_BAD_ACCESS on line NSString *submitStr = messageSubmit->submitTile;
.
有没有办法通过 iPhone/iPad 发送 NSString 消息?
Is there some way to send NSString message over iPhone/iPad?
推荐答案
你不能这样做:
NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageSubmit)];
...或者这个:
MessageSubmit * messageSubmit = (MessageSubmit *) [data bytes];
一般来说,您不能按原样发送对象的内存中表示.例如,该类的 submitTile
实例变量是一个指向 NSString
对象的指针.当您通过网络发送数据时,您不是在发送字符串本身,而是在发送指针——它只是发送设备上一块内存的内存地址.接收设备不会在任何地方存储相同的字符串,即使有,也不会具有相同的内存地址.所以你有一个无意义的指针指向任何地方,你期望它指向一个不存在的字符串.
Generally speaking, you can't just send the in-memory representation of objects around as-is. For example, the submitTile
instance variable of that class is a pointer to an NSString
object. When you send the data over the network, you aren't sending the string itself, you are sending the pointer - which is just a memory address of a chunk of memory on the sending device. The receiving device won't have the same string stored anywhere, and it wouldn't have the same memory address even if it did. So you've got a nonsensical pointer pointing to nowhere, and you're expecting it to point to a string that doesn't exist.
做你想做的最简单的方法是让你的 MessageSubmit
类 NSCoding
兼容.将其序列化为 NSData
而不仅仅是复制字节.
The easiest way of doing what you want to do is to make your MessageSubmit
class NSCoding
-compliant. Serialise it into NSData
instead of just making a copy of the bytes.
这篇关于通过游戏中心发送 NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!