objective c - Send NSString via Game Center -


i want send nsstring form 1 1 iphone/ipad via gamecenter crash exc_bad_access

here in .h file

typedef enum {     kmessagetyperandomnumber = 0,     kmessagetypegamebegin,     kmessagetypesubmit,     kmessagetypeexchange,     kmessagetypepickup,     kmessagetypepass,     kmessagetypegameover } messagetype;  typedef struct {     messagetype messagetype; } message;  typedef struct { message message; nsstring *submittile; } messagesubmit; 

and here in .m file

- (void)senddata:(nsdata *)data {     nserror *error;     bool success = [[gchelper sharedinstance].match senddatatoallplayers:data withdatamode:gkmatchsenddatareliable error:&error];     if (!success) {         cclog(@"error sending init packet");         [self matchended];     } } -(void)sendsubmit:(nsstring *) submittile {     messagesubmit message;     message.message.messagetype = kmessagetypesubmit;     message.submittile = submittile;     nsdata *data = [nsdata datawithbytes:&message length:sizeof(messagesubmit)];         [self senddata:data]; } 

and if click on ccmenu image call onsubmit function , here onsubmit function

-(void)onsubmit {     nsstring *submitstr = @"1-7-7 =-7-8 1-7-9";      [self sendsubmit:submitstr]; } 

and last 1 didreceivedata method

- (void)match:(gkmatch *)match didreceivedata:(nsdata *)data fromplayer:(nsstring *)playerid {     if (message->messagetype == kmessagetypesubmit) {         messagesubmit * messagesubmit = (messagesubmit *) [data bytes];         nsstring *submitstr = messagesubmit->submittile;          nslog(@"subtile %@",submitstr);     } } 

it have exc_bad_access on line nsstring *submitstr = messagesubmit->submittile;.

is there way send nsstring message on iphone/ipad?

you can't this:

nsdata *data = [nsdata datawithbytes:&message length:sizeof(messagesubmit)];     

...or this:

messagesubmit * messagesubmit = (messagesubmit *) [data bytes]; 

generally speaking, can't send in-memory representation of objects around as-is. example, submittile instance variable of class pointer nsstring object. when send data on network, aren't sending string itself, sending pointer - memory address of chunk of memory on sending device. receiving device won't have same string stored anywhere, , wouldn't have same memory address if did. you've got nonsensical pointer pointing nowhere, , you're expecting point string doesn't exist.

the easiest way of doing want make messagesubmit class nscoding-compliant. serialise nsdata instead of making copy of bytes.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -