• 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
Re: Using NSArchiver with NSDocument follow-up
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using NSArchiver with NSDocument follow-up


  • Subject: Re: Using NSArchiver with NSDocument follow-up
  • From: Weeks Family <email@hidden>
  • Date: Tue, 23 Sep 2003 00:03:35 -0400

My code example creates a mutable array (pages). Then creates & adds 4
mutable arrays (page) to pages. Each page gets MyObjects added to it
(see MyDocument.m init below). Here is the output during a save. Each
MyObject encodeWithCoder gets called twice. ??

2003-09-22 23:44:46.264 archive[3935] encodeWithCoder <MyObject:
0x19e0a0> 1
2003-09-22 23:44:46.265 archive[3935] encodeWithCoder <MyObject:
0x786d0> 1
2003-09-22 23:44:46.266 archive[3935] encodeWithCoder <MyObject:
0x1a1c70> 2
2003-09-22 23:44:46.266 archive[3935] encodeWithCoder <MyObject:
0x5aa90> 1
2003-09-22 23:44:46.266 archive[3935] encodeWithCoder <MyObject:
0x7b290> 2
2003-09-22 23:44:46.266 archive[3935] encodeWithCoder <MyObject:
0x1823c0> 3
2003-09-22 23:44:46.266 archive[3935] encodeWithCoder <MyObject:
0x796e0> 1
2003-09-22 23:44:46.267 archive[3935] encodeWithCoder <MyObject:
0x77ff0> 2
2003-09-22 23:44:46.267 archive[3935] encodeWithCoder <MyObject:
0x778f0> 3
2003-09-22 23:44:46.267 archive[3935] encodeWithCoder <MyObject:
0x178660> 4
2003-09-22 23:44:46.281 archive[3935] encodeWithCoder <MyObject:
0x19e0a0> 1
2003-09-22 23:44:46.281 archive[3935] encodeWithCoder <MyObject:
0x786d0> 1
2003-09-22 23:44:46.282 archive[3935] encodeWithCoder <MyObject:
0x1a1c70> 2
2003-09-22 23:44:46.282 archive[3935] encodeWithCoder <MyObject:
0x5aa90> 1
2003-09-22 23:44:46.283 archive[3935] encodeWithCoder <MyObject:
0x7b290> 2
2003-09-22 23:44:46.283 archive[3935] encodeWithCoder <MyObject:
0x1823c0> 3
2003-09-22 23:44:46.283 archive[3935] encodeWithCoder <MyObject:
0x796e0> 1
2003-09-22 23:44:46.284 archive[3935] encodeWithCoder <MyObject:
0x77ff0> 2
2003-09-22 23:44:46.284 archive[3935] encodeWithCoder <MyObject:
0x778f0> 3
2003-09-22 23:44:46.285 archive[3935] encodeWithCoder <MyObject:
0x178660> 4

Here is the output from open. Each initWithCoder gets called once.

2003-09-22 23:45:17.533 archive[3935] initWithCoder <MyObject:
0x13bac10> 1
2003-09-22 23:45:17.534 archive[3935] initWithCoder <MyObject:
0x12d8ee0> 1
2003-09-22 23:45:17.534 archive[3935] initWithCoder <MyObject:
0xf825c0> 2
2003-09-22 23:45:17.535 archive[3935] initWithCoder <MyObject:
0x13e1960> 1
2003-09-22 23:45:17.535 archive[3935] initWithCoder <MyObject:
0x12adfb0> 2
2003-09-22 23:45:17.535 archive[3935] initWithCoder <MyObject:
0x12ad190> 3
2003-09-22 23:45:17.535 archive[3935] initWithCoder <MyObject:
0xf7a620> 1
2003-09-22 23:45:17.535 archive[3935] initWithCoder <MyObject:
0xf7a3a0> 2
2003-09-22 23:45:17.535 archive[3935] initWithCoder <MyObject:
0x13b78e0> 3
2003-09-22 23:45:17.536 archive[3935] initWithCoder <MyObject:
0xf904a0> 4

Can anyone explain why MyObject encodeWithCoder gets called twice?

Here is the code:
//
// MyDocument.h
//
#import <Cocoa/Cocoa.h>

@interface MyDocument : NSDocument
{
NSMutableArray *pages;
}
@end
//
// MyDocument.m
//

#import "MyDocument.h"
#import "MyObject.h"

@implementation MyDocument

- (id)init
{
int i, j;
NSMutableArray *page;

self = [super init];
if (self) {
pages = [[NSMutableArray alloc] init];
for (i=1; i<=4; i++) {
page = [[NSMutableArray alloc] init];
[pages addObject:page];
for (j=1; j<=i; j++) {
[page addObject:[[MyObject alloc] initWithMyData:j]];
}
}
}
return self;
}

- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your
document supports multiple NSWindowControllers, you should remove this
method and override -makeWindowControllers instead.
return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the
windowController has loaded the document's window.
}

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
if ([aType isEqualToString:@"DocumentType"]) {
return [NSArchiver archivedDataWithRootObject:pages];
}
return nil;
}

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
NSMutableArray *unArchPages;
int i;

if ([aType isEqualToString:@"DocumentType"]) {
unArchPages = [NSUnarchiver unarchiveObjectWithData:data];
if (unArchPages) {
for (i=0; i<4; i++) {
[[pages objectAtIndex:i] setArray:[unArchPages
objectAtIndex:i]];
}
return YES;
}
}

return NO;
}

@end
/* MyObject */

#import <Cocoa/Cocoa.h>

@interface MyObject : NSObject <NSCoding>
{
int data;
}
- (id)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)coder;

- (id)initWithMyData:(int)value;
- (void)setData:(int)value;
- (int)data;
@end
#import "MyObject.h"

@implementation MyObject

- (id)initWithMyData:(int)value
{
[self setData:value];
return self;
}

- (id)initWithCoder:(NSCoder *)coder
{
[coder decodeValueOfObjCType:@encode(int) at:&data];
NSLog(@"initWithCoder %@ %i", self, [self data]);
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeValueOfObjCType:@encode(int) at:&data];
NSLog(@"encodeWithCoder %@ %i", self, [self data]);
}

- (void)setData:(int)value
{
data = value;
}

- (int)data
{
return data;
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: What ever happened to "shared frameworks"?
  • Next by Date: Saving Plug-in Data into Document?
  • Previous by thread: RE: Using NSArchiver with NSDocument follow-up
  • Next by thread: NSImage initialized with EPS file is nil
  • Index(es):
    • Date
    • Thread