使用 GameKit 以字节形式发送和接收 NSMutableArray [英] Sending and receive NSMutableArray as Bytes using GameKit

查看:67
本文介绍了使用 GameKit 以字节形式发送和接收 NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个玩家向另一个玩家发送一个数组.

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;

并尝试发送:

-(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 写的有点帮助:

However no luck, so I went looking around and found that @rwenderlich wrote a little helpful bit:

  1. 将 NSArray 转换为 NSData

  1. Convert NSArray to NSData

在struct中,存储一个unsigned int numBytes和一个void * bytes

In struct, store an unsigned int numBytes, and a void * bytes

在numBytes中,写出NSData的长度,然后写出NSData的内容

In numBytes, write the length of the NSData, and then write out the contents of the NSData

另一方面,读取 numBytes 以查看接下来要读取的字节数,然后读取该数量的字节并反转该过程(bytes->NSData->NSArray).

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).

我在 1 和 2 上做得很好...我相信,但我在 3 和 4 上迷路了.有人可以帮我把它翻译成有意义的代码吗?

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?

谢谢!

推荐答案

你可以只使用 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 GameState : NSObject
{
   //enum for session state
    SessionState _currentState;
    NSData *_data;
    NSString *_message;

    NSDate *_timestamp;

}

@property(nonatomic,retain) NSDate *timestamp;

@property(assign) SessionState currentState;
@property(nonatomic,retain) NSData *data;
@property(nonatomic,retain) NSString *message;
- (id)initWithCoder:(NSCoder *)coder;

实施

@implementation GameState
@synthesize message=_message ,  currentState= _currentState, data=_data;
@synthesize  timestamp=_timestamp;
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        _killPlayback=FALSE;
        _lastPacket=FALSE;
        _number=0.0;
        _pass=FALSE;
        _timestamp=NULL;

    }

    return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:_timestamp forKey:@"Timestamp"];

    [coder encodeObject:_message forKey:@"Message"];
    [coder encodeObject:_data forKey:@"Data"];
    [coder encodeInt:_currentState forKey:@"State"];



}
// Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    _message  = [[coder decodeObjectForKey:@"Message"] retain];
    _data  = [[coder decodeObjectForKey:@"Data"] retain];
    _currentState=(SessionState)[coder decodeIntForKey:@"State"];
    _timestamp=[[coder decodeObjectForKey:@"Timestamp"] retain];
    return self;
}
@end

现在你可以像这样从对象中获取数据

now you can get data from the object like

-(NSData*)dataFromObject:(id)obj
{
    return [NSKeyedArchiver archivedDataWithRootObject:obj];
}

和来自数据的对象

-(id)objectFromData:(NSData*)data
{
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

你的工作流程变成在发送方

and your workflow becomes on the sender side

GameState *state=..;
NSData* data=[self dataFromObject:state];
//send the data

在接收方

GameState *state=[self objectFromData:data];

希望能帮到你

这篇关于使用 GameKit 以字节形式发送和接收 NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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