2011-07-26 48 views
3

我正在使用AsyncSocket的客戶端/服務器應用程序。爲傳輸數據,它使用NSData來自自定義對象的NSData

如何我可以插入我的自定義對象,包含NSNumber S,NSInteger s和NSString s轉換爲NSData對象,然後拿回來了呢?

回答

5

將自定義對象插入(序列化)到NSData對象的一種方法是使用NSCoding和NSKeyedArchiver。

首先,讓您的自定義對象實現NSCoding協議。
例這裏: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Articles/codingobjects.html#//apple_ref/doc/uid/20000948-97234

然後,使用具有的NSKeyedArchiver你的對象信息,請參閱:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Articles/creating.html

希望幫助!