我尝试通过GameKit以C Struct的形式发送NSArray,结构如下所示:
typedef struct {
Message message;
unsigned int numBytes;
Byte* bytes;
} MessageBlock;
这里我尝试获取数组的字节并将它们放入bytes变量:
- (void)sendBlock:(NSMutableArray*)arrayBlocks {
MessageBlock messageBlock;
messageBlock.message.messageType = kMessageTypeBlock;
NSData* arrayData = [NSKeyedArchiver archivedDataWithRootObject:arrayBlocks];
messageBlock.numBytes = sizeof(arrayData);
[arrayData getBytes:&messageBlock.bytes length:messageBlock.numBytes];
NSData *data = [NSData dataWithBytes:&messageBlock
length:sizeof(MessageBlock)];
[self sendData:data];
}
Soo发送工作,但当我尝试检索这样的数组时:
else if (message->messageType == kMessageTypeBlock)
{
MessageBlock *messageBlock = (MessageBlock*)[data bytes];
NSMutableArray* arrayBlocks = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithBytes:messageBlock->bytes length:messageBlock->numBytes]];
[self.delegate insertBlock:arrayBlocks];
}
代码中断了EXC_BAD_ACCESS异常。我的猜测:我只是发送指针而不是实际的字节:/ 所以我的问题是:如何将NSData的字节输入结构的bytes变量? 谢谢你的帮助!