Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:21:08+00:00 2026-05-29T05:21:08+00:00

I am trying to send an Array from one player to another. I create

  • 0

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;

And try to send with this:

-(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];
}

And receive with this:

if (message->messageType == kMessageTypeRecievedRadarPing){

        MessagePingBackToCaller *messageSent = (MessagePingBackToCaller *)[data bytes];

        NSLog(@"Data : %@", messageSent->bytes);

        NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:messageSent->bytes];
        NSLog(@"Results : %@", array);
    }

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

  1. Convert NSArray to NSData

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

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

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

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?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:21:09+00:00Added an answer on May 29, 2026 at 5:21 am

    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

    @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

    @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];
    }
    

    and an object from the data

    -(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
    

    and on the receiving side

    GameState *state=[self objectFromData:data];
    

    hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a struct to a char array to send over the
I'm trying to sum reduce an array from a kernel without needing to send
i'm trying to send three arrays of data from one .js file which is
I'm trying to use jQuery's $getJSON to send an array of 'ids'. Here's what
I am trying to send an email from a site I am building, but
I am trying to send an email via GMail's SMTP server from a PHP
I am trying to get JSON data loaded from a NSURLConnection delegate to send
I'm trying to send an array of objects wrapped in an array object wrapper
I'm having a problem trying to send an array of ints to a .NET
I'm trying to create a very simple database abstraction, one part of it using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.