Hi there,
I'm working on a project that involves sending and receiving NSData objects over a local network. Right now, I know that my NSData object is actually an NSString, and when it is received I can easily convert it to one because I know exactly what it is.
However, what if I want to distinguish between an NSString or NSDictionary (or anything else for that matter)? Is there a way I can take an NSData object and figure out what's actually inside it?
Here's what I'm talking about:
It's easy right now since I know my data contains a string. What if it contains an NSDictionary instead? Is there a way I can test for this in my code and react accordingly?
I hope that makes sense, thanks!
I'm working on a project that involves sending and receiving NSData objects over a local network. Right now, I know that my NSData object is actually an NSString, and when it is received I can easily convert it to one because I know exactly what it is.
However, what if I want to distinguish between an NSString or NSDictionary (or anything else for that matter)? Is there a way I can take an NSData object and figure out what's actually inside it?
Here's what I'm talking about:
Code:
// Sender Code
NSString *message = @"My message";
const char *utfString = [message UTF8String];
NSData *myData = [NSData dataWithBytes: utfString length: strlen(utfString)];
[socket sendData:myData toHost:broadcastAddress port:broadcastPort withTimeout:-1 tag:0]; // Send my message
Code:
// Receiver Code
NSString *receivedData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // Initialize an NSString with the received data
It's easy right now since I know my data contains a string. What if it contains an NSDictionary instead? Is there a way I can test for this in my code and react accordingly?
I hope that makes sense, thanks!