使用 GameKit 以字节形式发送和接收 NSMutableArray
我正在尝试将数组从一个玩家发送到另一个玩家。
我在标头中创建了一个结构体
typedef struct {
Message message;
unsigned int numBytes;
void * bytes;
} MessagePingBackToCaller;
typedef struct {
MessageType messageType;
} Message;
并尝试用这个发送:
-(void)sendOpponentRadar{
MessagePingBackToCaller sendMessage;
sendMessage.message.messageType = kMessageTypeRecievedRadarPing;
NSData *pingData = [NSKeyedArchiver archivedDataWithRootObject:[cover gatherAndReturnColumnNumbers]];
sendMessage.bytes = pingData;
NSData *data = [NSData dataWithBytes:&sendMessage length:sizeof(MessagePingBackToCaller)];
[self sendData:data];
}
并用这个接收:
if (message->messageType == kMessageTypeRecievedRadarPing){
MessagePingBackToCaller *messageSent = (MessagePingBackToCaller *)[data bytes];
NSLog(@"Data : %@", messageSent->bytes);
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:messageSent->bytes];
NSLog(@"Results : %@", array);
}
但是没有运气,所以我环顾四周,发现@rwenderlich写了一些有用的位:
Convert NSArray to NSData
在 struct 中,存储一个 unsigned int numBytes 和一个 void * bytes
In numBytes,写入NSData的长度,然后写出NSData的内容
另一边,读取numBytes 来查看接下来要读取多少字节,然后读入该数量的字节并反转该过程(bytes->NSData->NSArray)。
我对 1 和 2 非常擅长...我相信,但我在 3 和 4 上迷失了。有人可以帮我将其翻译成有意义的代码吗?
谢谢!
I am trying to send an Array from one player to another.
I create a struct in the header
typedef struct {
Message message;
unsigned int numBytes;
void * bytes;
} MessagePingBackToCaller;
typedef struct {
MessageType messageType;
} Message;
And try to send with this:
-(void)sendOpponentRadar{
MessagePingBackToCaller sendMessage;
sendMessage.message.messageType = kMessageTypeRecievedRadarPing;
NSData *pingData = [NSKeyedArchiver archivedDataWithRootObject:[cover gatherAndReturnColumnNumbers]];
sendMessage.bytes = pingData;
NSData *data = [NSData dataWithBytes:&sendMessage length:sizeof(MessagePingBackToCaller)];
[self sendData:data];
}
And receive with this:
if (message->messageType == kMessageTypeRecievedRadarPing){
MessagePingBackToCaller *messageSent = (MessagePingBackToCaller *)[data bytes];
NSLog(@"Data : %@", messageSent->bytes);
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:messageSent->bytes];
NSLog(@"Results : %@", array);
}
However no luck, so I went looking around and found that @rwenderlich wrote a little helpful bit:
Convert NSArray to NSData
In struct, store an unsigned int numBytes, and a void * bytes
In numBytes, write the length of the NSData, and then write out the contents of the NSData
On the other side, read numBytes to see how many bytes to read next, then read in that amount of bytes and reverse the process (bytes->NSData->NSArray).
Im pretty good on 1 and 2... I believe, but i get lost on 3 and 4. Could someone please help me translate this into meaningful code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 NSKeyedArchiver 来完成这个技巧,不要使用 c 结构,使其成为 Objective-C 类,使用 NSKeyedArchiver 从事物中获取数据并发送该数据,然后在另一端取消存档,这是任意类的示例
接口
实现
现在您可以从对象中获取数据,例如
从数据中获取对象
,您的工作流程就变成了
发送方
和接收方
希望对您有所帮助
You can just use NSKeyedArchiver to do the trick, dont use the c struct, make it an objective-c class, get data from the thing using NSKeyedArchiver and send that, then just unarchive in the other side, heres an example for an arbitrary class
interface
implementation
now you can get data from the object like
and an object from the data
and your workflow becomes
on the sender side
and on the receiving side
hope it helps
您还可以尝试以下方法来获取压缩数据(大约是原始大小的 1/5):
You might also try the following to get compressed data (about 1/5 the size of the original):