• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Newbie Q : Not-so-simple encoding&decoding example
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Newbie Q : Not-so-simple encoding&decoding example


  • Subject: Newbie Q : Not-so-simple encoding&decoding example
  • From: email@hidden
  • Date: Fri, 26 May 2006 11:16:49 +0200 (CEST)
  • Importance: Normal

  Hello all,

 I've read many tutorials on the way to encode and decode objects in Cocoa,
but they all share one limitation : they only treat the simplest
"one class-one data type" case. When I tried to adapt it to the slightly more
complicated example below I failed.  Can anyone please tell me what should
be corrected
in the encoding & decoding methods below ?

                                                                                Ewan


   Three classes : MyDocument, Class1, Class2

Here are the interfaces :

@interface Class1 : NSObject <NSCoding> {
                 NSMutableArray* array1;
                 NSMutableArray* array2;
}
@end

@interface Class2 : NSObject <NSCoding> {
                 NSMutableArray* array3;
                 NSMutableArray* array4;
                 Class1* class1;
}
@end

@interface MyDocument : NSDocument {

                Class2* class2;
}
@end

and here are the coding methods :

In Class1.m

-- (id)initWithCoder:(NSCoder *)coder
{
    if (self = [super init]) {
             [array1 release];
			 [array2 release];

			 array1=[[coder decodeObject] retain];
			 array2=[[coder decodeObject] retain];

    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:array1];
    [coder encodeObject:array2];

}


In Class2.m

-- (id)initWithCoder:(NSCoder *)coder
{
    if (self = [super init]) {
             [array3 release];
			 [array4 release];
             [class1 release];

			 array3=[[coder decodeObject] retain];
			 array4=[[coder decodeObject] retain];
             class1=[[coder decodeObject] retain];

    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:array3];
    [coder encodeObject:array4];
    [coder encodeObject:class1];

}

In MyDocument.m :

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
        return [NSArchiver archivedDataWithRootObject:class2];
}


- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{

    [class2 release];
    class2 = [[NSUnarchiver unarchiveObjectWithData:data] retain];

    return YES;
}



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Newbie Q : Not-so-simple encoding&decoding example
      • From: James Bucanek <email@hidden>
    • Re: Newbie Q : Not-so-simple encoding&decoding example
      • From: Uli Kusterer <email@hidden>
  • Prev by Date: Re: [Q] which arrives earlier? applicationShouldTerminate or applicationWillTerminate
  • Next by Date: Capturing audio from input and saving
  • Previous by thread: problem During Line drawing.
  • Next by thread: Re: Newbie Q : Not-so-simple encoding&decoding example
  • Index(es):
    • Date
    • Thread