iOS Development: How can I encapsulate a string in an NSData object?

Posted by BeachRunnerJoe on Stack Overflow See other posts from Stack Overflow or by BeachRunnerJoe
Published on 2011-01-08T17:04:53Z Indexed on 2011/01/08 17:54 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

Hello. I'm building a multiplayer game on the iPhone and I need to send string data to the other players in the game. To do that, I need to encapsulate my NSString* string data in an NSData object somehow. Here's an example of how my code is structured...

typedef struct 
{
   PACKETTYPE packetType;
   ??? stringToSend;  //<---not sure how to store this
} StringPacket;    

StringPacket msg;
msg.packetType = STRING_PACKET;
msg.stringToSend = ...  //  <---not sure what to do here
NSData *packet = [NSData dataWithBytes:&msg length:sizeof(StringPacket)];

So my question is, if StringPacket is a struct defined in my header, what type should the stringToSend property be so that I can easily call the dataWithBytes method of NSData to encapsulate the packet data in an NSData object?

Thanks for your wisdom!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about ipad