0

Currently I am able to write and receive data containing a String by doing this:

NSString *anyString = @"anyString";
NSData *data = [anyString dataUsingEncoding:NSUTF8StringEncoding];
[self.outputStream write:[data bytes] maxLength:[data length]];

But is it possible to put an object in the data and write it to the outputStream? If so, how? If it's not possible, should I just pass a unique identifier(NSString) through the socket and compare with my list of objects until I find a matching?

Thanks in advance.

1 Answers1

0

You can implement NSCoding protocol. Then you can serialize your objects with NSKeyedArchiver and use JSONKit to transfer the data to remote servers.

Adam
  • 26,549
  • 8
  • 62
  • 79
  • I have implemented NSCoding protocol for that object, and imported the JSONKit files, but I can't find any resources on how to pass/give the "NSData *data = [NSKeyedArchiver archivedDataWithRootObject:someObject]" to the JSONKit. Any ideas or should I just make it a separate question? – ObjectiveC-InLearning Jun 03 '12 at 21:09
  • JSONKit should provide category for NSData. I can't provide any example now as I'm typing this from iPhone. – Adam Jun 03 '12 at 21:13
  • 1
    Note that serialization is just passing the data of an object, similar to the string in the question. True, it can be a more complicated data structure, but the thing which makes objects objects and not just data structure is behavior – that is, code. You can't really transmit code. The receiver already has to have the code. If it has the class definition, then it can reconstruct an object from the serialized data. If it doesn't, then it can't. – Ken Thomases Jun 03 '12 at 22:02