Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structs to NSData to Structs?

I have the following struct:

typedef enum
{
    kPacketTypeJoin = 1,
    kPacketTypeServerAcceptedJoin,
    kPacketTypeSelectedNumber,
} PacketTypes;    
typedef struct
{
    PacketTypes type;
    NSString *name;
} PacketJoin;

how would I convert a PacketJoin into NSData and back to PacketJoin again?

like image 477
ninjaneer Avatar asked Mar 21 '11 03:03

ninjaneer


1 Answers

// make a NSData object
NSData *myData = [NSData dataWithBytes:&myPacketJoin length:sizeof(myPacketJoin)];

// make a new PacketJoin
PacketJoin newJoin;
[myData getBytes:&newJoin length:sizeof(newJoin)];
like image 88
Caleb Avatar answered Nov 05 '22 09:11

Caleb