开发者

how to differentiate between NSString object converted into NSData and UIImage object converted into NSData

i am developing an application in which i will be sending a string to peer(after converting it i开发者_开发百科nto NSData object) and also an UIImage again by converting into NSData object.

Now at receiving end i have a receive method

- (void) receiveData:(NSData *)data
            fromPeer:(NSString *)peer
           inSession:(GKSession *)session
             context:(void *)context

My problem is that,same receive method is called every time i send any data,and i have to use image and text data differently.

So how can i know,whether i data that i have received is a NSString object converted into NSData or it's UIImage data converted into NSData.

please help me.


Take a look at Apple's GKRocket sample code, specifically the implementation of sendData:ofType and receiveData:fromPeer:inSession:context: in SessionManager.m. You will need to define a set of "types" specific to your application and send them as a header in the NSData payload, and then read the header on the receiving end to determine how to handle the payload appropriately.


The easiest way is to define another parameter (ofType) in recieveData that distinguishes what type of data is being sent:

(void) receiveData:(NSData *)data ofType: (bool type) fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context 


The context parameter is supposed to be arbitrary data sent to provide a context or meaning for the sent data. It can be anything converted to data, even complex objects.

In this case a simple bool value would do.

The above is incorrect. The context is set by the receiver to distinguish between multiple sessions or peers. -- TechZen

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜