通过游戏中心发送NSString [英] Send NSString via Game Center

查看:136
本文介绍了通过游戏中心发送NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Gamecenter
向另一个iPhone / iPad发送NSString,但它与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文件



here in .h file

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

它的EXC_BAD_ACCESS行 NSString * submitStr = messageSubmit-> submitTile;

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 class 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆