我有以下结构:
typedef enum
{
kPacketTypeJoin = 1,
kPacketTypeServerAcceptedJoin,
kPacketTypeSelectedNumber,
} PacketTypes;
typedef struct
{
PacketTypes type;
NSString *name;
} PacketJoin;
我如何将PacketJoin转换为NSData并再次转换回PacketJoin?
答案 0 :(得分:38)
// make a NSData object
NSData *myData = [NSData dataWithBytes:&myPacketJoin length:sizeof(myPacketJoin)];
// make a new PacketJoin
PacketJoin newJoin;
[myData getBytes:&newJoin length:sizeof(newJoin)];