<small id='eLKqc'></small><noframes id='eLKqc'>

      1. <i id='eLKqc'><tr id='eLKqc'><dt id='eLKqc'><q id='eLKqc'><span id='eLKqc'><b id='eLKqc'><form id='eLKqc'><ins id='eLKqc'></ins><ul id='eLKqc'></ul><sub id='eLKqc'></sub></form><legend id='eLKqc'></legend><bdo id='eLKqc'><pre id='eLKqc'><center id='eLKqc'></center></pre></bdo></b><th id='eLKqc'></th></span></q></dt></tr></i><div id='eLKqc'><tfoot id='eLKqc'></tfoot><dl id='eLKqc'><fieldset id='eLKqc'></fieldset></dl></div>
        <legend id='eLKqc'><style id='eLKqc'><dir id='eLKqc'><q id='eLKqc'></q></dir></style></legend>
        <tfoot id='eLKqc'></tfoot>
        • <bdo id='eLKqc'></bdo><ul id='eLKqc'></ul>
      2. 通过游戏中心发送 NSString

        时间:2024-08-12
        <tfoot id='WV7Je'></tfoot>
        • <small id='WV7Je'></small><noframes id='WV7Je'>

            • <i id='WV7Je'><tr id='WV7Je'><dt id='WV7Je'><q id='WV7Je'><span id='WV7Je'><b id='WV7Je'><form id='WV7Je'><ins id='WV7Je'></ins><ul id='WV7Je'></ul><sub id='WV7Je'></sub></form><legend id='WV7Je'></legend><bdo id='WV7Je'><pre id='WV7Je'><center id='WV7Je'></center></pre></bdo></b><th id='WV7Je'></th></span></q></dt></tr></i><div id='WV7Je'><tfoot id='WV7Je'></tfoot><dl id='WV7Je'><fieldset id='WV7Je'></fieldset></dl></div>

                <legend id='WV7Je'><style id='WV7Je'><dir id='WV7Je'><q id='WV7Je'></q></dir></style></legend>
                  <bdo id='WV7Je'></bdo><ul id='WV7Je'></ul>
                    <tbody id='WV7Je'></tbody>

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

                  问题描述

                  我想通过 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.

                  做你想做的最简单的方法是让你的 MessageSubmitNSCoding 兼容.将其序列化为 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Box2D 中获取世界的contactListener 下一篇:cocos2d v3 在应用程序使用期间重新定向屏幕

                  相关文章

                • <small id='Tqv9l'></small><noframes id='Tqv9l'>

                    <legend id='Tqv9l'><style id='Tqv9l'><dir id='Tqv9l'><q id='Tqv9l'></q></dir></style></legend>

                  1. <tfoot id='Tqv9l'></tfoot>
                    <i id='Tqv9l'><tr id='Tqv9l'><dt id='Tqv9l'><q id='Tqv9l'><span id='Tqv9l'><b id='Tqv9l'><form id='Tqv9l'><ins id='Tqv9l'></ins><ul id='Tqv9l'></ul><sub id='Tqv9l'></sub></form><legend id='Tqv9l'></legend><bdo id='Tqv9l'><pre id='Tqv9l'><center id='Tqv9l'></center></pre></bdo></b><th id='Tqv9l'></th></span></q></dt></tr></i><div id='Tqv9l'><tfoot id='Tqv9l'></tfoot><dl id='Tqv9l'><fieldset id='Tqv9l'></fieldset></dl></div>
                    • <bdo id='Tqv9l'></bdo><ul id='Tqv9l'></ul>