What's in my NSData?

Discussion in 'Mac Programming' started by jeremyapp, Jun 28, 2010.

  1. jeremyapp macrumors newbie

    Joined:
    Apr 30, 2008
    #1
    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:

    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!
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    NSData simply stores a buffer of bytes. Without additional metadata, it's impossible to know for sure what it contains, because the contents could be re-intepreted a million different ways. There are ways to make good guesses about what the intended interpretation of data is. For example, many images have "magic numbers" at the front. Or you could detect an ASCII string by seeing if most of the characters' codes lie within the normal ASCII range. There's certainly no general API for this; you'd have to implement it yourself.

    However, there IS an API for encoding and decoding objects which may be what you're actually looking for. See NSCoding and NSKeyedArchiver.

    (By the way, there's a bug in your code. In the "sender" code, your char * is UTF8-encoded and in your "receiver" code, you create your string with an ASCII encoding. The two should match each other.)
     
  3. jeremyapp thread starter macrumors newbie

    Joined:
    Apr 30, 2008
    #3
    Thanks for the help!

    What I ended up doing was creating an NSDictionary with my object and also a key for the content type. I then archive it and send it as NSData. When I receive it, I have all the information I need in that NSDictionary.

    Thanks!

    PS - Good catch on the string encoding, fixed that too.
     
  4. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #4
    You can also try to use isKindOfClass. After you receive the NSData, you can instantiate an object and test it if it is valid. Just a thought!
     

Share This Page