This is NSObjCTypeSerializationCallBack.rtf in view mode; [Download] [Up]
paperh18648 margl-907 margr0 margt0 margb0 fi0 ri0 ql sb0 f1 fs24 Copyright f3 'e3f1 1994 by NeXT Computer, Inc. All Rights Reserved. s4 li100 fi0 ri1007 ql f0 b fs36 fs100 fs36 NSObjCTypeSerializationCallBack pard s0 li2872 tx2872 f1 b0 fs28 fs48 fs28 f0 b fs24 Adopted By:tab b0 fs28 f1 No OpenStep classes fs20 fs28 s7 fi-2771 ri1007 ql f0 b fs24 Declared In:tab b0 fs28 f1 Foundation/NSSerialization.h fs20 fs28 pard s16 li100 fi0 ri1007 ql f0 b fs28 fs20 fs28 Protocol Description fs14 fs28 pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 b0 fs2 fs28 An object conforms to the NSObjCTypeSerializationCallBack protocol so that it can intervene in the serialization and deserialization process. The primary purpose of this protocol is to allow for the serialization of objects and other data types that aren'27t directly supported by OpenStep'27s serialization facility. (See the NSSerializer class specification for information on serialization.) fs16 fs28 NSMutableData declares the method that'27s used to begin the serialization process: fs16 fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 - (void)serializeDataAt:(const void *)data f1 fs28 f2 fs20 ofObjCType:(const char *)type tab f1 fs28 f2 fs20 context:(id <NSObjCTypeSerializationCallBack>)callback pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 fs28 This method can serialized all standard Objective C types (b intb0 , b floatb0 , character strings, and so on) except for objects, b unionb0 , and b void *b0 . If, during the serialization process, an object is encountered, the object passed as the callback argument above is asked to provide the serialization. fs16 fs28 Suppose that the type being serialized is a structure of this description: fs16 fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 struct stockRecord { f1 fs28 f2 fs20 NSString *stockName; f1 fs28 f2 fs20 float value; }; pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 fs28 The Objective C type code for this structure is {@f}, so the serialization process begins with this message: (Assume that b theDatab0 is the NSMutableData object that'27s doing the serialization and b helperb0 is an object that conforms to the NSObjCTypeSerializationCallBack protocol.) fs16 fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 struct stockRecord aRecord = {@f1 fs28 "f2 fs20 aCompanyf1 fs28 "f2 fs20 , 34.7}; [theData serializeDataAt:&aRecord ofObjCType:f1 fs28 "{@f}"f2 fs20 context:helper]; pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 fs28 Since the first field of the structure is an unsupported type, the helper object is sent a b serializeObjectAt:ofObjCType:intoData:b0 message, letting it serialize the object. b helperb0 might implement the method in this way: fs16 fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 - (void)serializeObjectAt:(id *)objectPtr ofObjCType:(const char *)type intoData:(NSMutableData *)theMutableData { NSString *nameObject; char *companyName nameObject = *objectPtr; companyName = [nameObject cString]; [theData serializeDataAt:&companyName ofObjCType:@encode(typeof(companyName)) context:nil] } pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 fs28 The callback object is free to serialize the target object as it wishes. In this case, b helperb0 simply extracts the company name from the NSString object and then has that character string serialized. Once this callback method finishes executing, the original method (b serializeDataAt:ofObjCType:context:b0 ) resumes execution and serializes the second field of the structure. Since this second field contains a supported type (b floatb0 ), the callback method is not invoked again. fs16 fs28 Deserialization follows a similar pattern, except in this case NSData declares the central method b deserializeDataAt:ofObjCType:atCursor:context:b0 . The deserialization of the example structure starts with a message to the NSData object that contains the serialized data: fs16 fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 (unsigned *)cursor = 0; [theData deserializeDataAt:&aRecord ofObjCType:f1 fs28 "{@f}"f2 fs20 cursor:&cursorf1 fs28 f2 fs20 context:helper]; pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 fs28 (The cursor argument is a pointer to zero since we'27re starting at the beginning of the data in the NSData object.) fs16 fs28 When this method is invoked, the callback object receives a b deserializeObjectAt:ofObjCType:fromData:atCursor:b0 message, as declared in this protocol. The callback object can then reestablish the first field of the structure. For example, b helperb0 might implement the method in this way: fs16 fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 - (void) deserializeObjectAt:(id *)objectPtr f1 fs28 f2 fs20 ofObjCType:(const char *)type f1 fs28 f2 fs20 fromData:(NSData *)data f1 fs28 f2 fs20 atCursor:(unsigned *)cursor { f1 fs28 f2 fs20 char *companyName; f1 fs28 f2 fs20 [theData deserializeDataAt:&companyName ofObjCType:f1 fs28 "f2 fs20 *f1 fs28 " f2 fs20 atCursor:cursor context:nil]; f1 fs28 f2 fs20 *objectPtr = [[NSString stringWithCString:companyName] retain]; } s16 li100 fi1130 fi0 f0 b fs28 fs48 fs28 Callback Handling fs14 fs28 pard s8 li7029 fi-6552 ri1007 ql tx6652 tx7030 f1 b0 fs2 fs28 fi-6552 {f3 -} (void)b deserializeObjectAt:b0 (id *)i objecti0 tab The implementor of this method decodes the referenced s9 li7030 fi-6553 fi-5796 b ofObjCType:b0 (const char *)i typei0 tab tab i objecti0 (which should always be of i typei0 "@") located at b fromData:b0 (NSData *)i datai0 tab tab thei cursori0 position in the i datai0 object. The decoded b atCursor:b0 (unsigned int*)i cursori0 tab tab object is i noti0 autoreleased. See description of NSData method b deserializeDataAt:ofObjCType:context:b0 . s8 li7029 fi-5794 fi-6552 fs16 fs28 fi-6552 {f3 -} (void)b serializeObjectAt:b0 (id *)i objecti0 tab The implementor of this method encodes the referenced s9 li7030 fi-6553 fi-5796 b ofObjCType:b0 (const char *)i typei0 tab tab i objecti0 (which should always be of i typei0 "@") in the i data i0 b intoData:b0 (NSMutableData *)i datai0 tab tab object. See description of NSMutableData method b serializeDataAt:ofObjCType:context:b0 }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.