Note that there are some explanatory texts on larger screens.

plurals
  1. POSending Data via Message Object Using NSKeyedArchiver
    primarykey
    data
    text
    <p>I'm implementing a multiplayer game where I have to send data between players using:</p> <pre><code> [self sendData:data mode:GKMatchSendDataReliable]; </code></pre> <p>Instead of using multiple c-structs like in this tutorial (<a href="http://www.raywenderlich.com/3325/how-to-make-a-simple-multiplayer-game-with-game-center-tutorial-part-22" rel="nofollow">http://www.raywenderlich.com/3325/how-to-make-a-simple-multiplayer-game-with-game-center-tutorial-part-22</a>), I created a Message object and a subclass of Message called Submessage. The subclasses are created so I avoid sending unnecessary variables in each message, to minimize the size of each packet sent. I then used NSCoding to encode and decode the messages. </p> <p>The classes look something like the following:</p> <p><strong>Message.h</strong></p> <pre><code> @interface Message : NSObject &lt;NSCoding&gt; @property int messageType; - (void)encodeWithCoder:(NSCoder*)coder; - (id)initWithCoder:(NSCoder*)coder; </code></pre> <p><strong>Message Coder Methods</strong></p> <pre><code>-(void) encodeWithCoder:(NSCoder *)coder{ [coder encodeInt:messageType forKey:@"messageType"]; } -(id)initWithCoder:(NSCoder *)coder { self = [super init]; messageType = [coder decodeIntForKey:@"messageType"]; return self; } </code></pre> <p><strong>Submessage.h</strong></p> <pre><code> @interface Submessage : Message &lt;NSCoding&gt; @property float additionalParameter1; - (void)encodeWithCoder:(NSCoder*)coder; - (id)initWithCoder:(NSCoder*)coder; </code></pre> <p><strong>Submessage Coder Methods</strong></p> <pre><code>- (void)encodeWithCoder:(NSCoder *)coder{ [super encodeWithCoder:coder]; [coder encodeFloat:additionalParameter1 forKey:@"float"]; } - (id)initWithCoder:(NSCoder *)coder { self = [super init]; [self initWithCoder:coder]; floatParameter1 = [coder decodeFloatForKey:@"floatParameter1"]; return self; } </code></pre> <p>After I receive the data, I use the following method to unarchive it:</p> <pre><code> Message *thisMessage = [NSKeyedUnarchiver unarchiveObjectWithData:data]; </code></pre> <p>However, this doesn't work when I'm sending Submessage. How can I determine whether data is of type Message or Submessage? How should I implement this so that it would work like in the c-struct case?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload