Re: Using NSArchiver with NSDocument follow-up - Code example
Re: Using NSArchiver with NSDocument follow-up - Code example
- Subject: Re: Using NSArchiver with NSDocument follow-up - Code example
- From: Weeks Family <email@hidden>
- Date: Mon, 22 Sep 2003 07:39:58 -0400
Sure. The mutable array is pages. It actually holds mutable arrays,
one for each page. The page mutable arrays hold the objects for each
page. My documents only need to be 4 pages, so I pre-create the pages
with 4 page arrays for simplicity.
The header file declaration is:
NSMutableArray *pages;
and the code is:
- (id)init
{
int i;
self = [super init];
if (self) {
pages = [[NSMutableArray alloc] init];
for (i=0; i<PageMax; i++) {
[pages addObject:[[NSMutableArray alloc] init]];
}
[self setCurrentPage:PageZero];
}
return self;
}
- (void)dealloc
{
int i;
// delete elements in all pages
for (i=0; i<PageMax; i++) {
[[pages objectAtIndex:i] removeAllObjects];
}
// delete each page
[pages removeAllObjects];
// now delete the pages array
[pages release];
[super dealloc];
}
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
if ([aType isEqualToString:@"MyDoc"]) {
return [NSArchiver archivedDataWithRootObject:pages];
}
return nil;
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
NSMutableArray *unArchPages;
int i;
if ([aType isEqualToString:@"MyDoc"]) {
unArchPages = [NSUnarchiver unarchiveObjectWith
Data:data];
if (unArchPages) {
for (i=0; i<PageMax; i++) {
[[pages objectAtIndex:i] setArray:[unArchPages
objectAtIndex:i]];
}
[self setCurrentPage:PageZero];
return YES;
}
}
return NO;
}
On Monday, September 22, 2003, at 01:59 AM, Scott Anguish wrote:
>
any code you can provide?
>
>
>
On Sep 21, 2003, at 5:00 PM, Weeks Family wrote:
>
>
> I've got two responses to my original post. Thanks.
>
>
>
> So I understand now that NSDocument subclasses should not implement
>
> the
>
> the NSCoding protocol. I changed my doc subclass so that a
>
> NSMutableArray holds all the data. And I removed the initWithCoder,
>
> encodeWithCoder methods from my doc subclass. Now the doc subclass
>
> dataRepresentationOfType invokes archivedDataWithRootObject with the
>
> mutable array. I can save and open files now.
>
>
>
> The encodeWithCoder still gets called twice for each of the objects in
>
> the mutable array. I do not understand this. The initWithCoder gets
>
> called once, this sounds correct.
>
>
>
> Can anyone explain this?
>
>
>
> Thanks.
>
> John Weeks
>
> _______________________________________________
>
> 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.
_______________________________________________
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.