Re: encodeConditionalObject:
Re: encodeConditionalObject:
- Subject: Re: encodeConditionalObject:
- From: Ondra Cada <email@hidden>
- Date: Fri, 2 Aug 2002 17:37:50 +0200
On Friday, August 2, 2002, at 03:49 , Anthony Arthur wrote:
Anyone have an example of how to use encodeConditionalObject:?
Sure:
17 /tmp> cat q.m
#import <Foundation/Foundation.h>
@interface Foo:NSObject <NSCoding> {
@public // no need for accessors for this example
id delegate;
}
@end
@implementation Foo
-(void)encodeWithCoder:(NSCoder*)coder {
[coder encodeConditionalObject:delegate];
return;
}
-initWithCoder:(NSCoder*)coder {
self=[super init];
delegate=[coder decodeObject];
return self;
}
-(NSString*)description {
return [NSString stringWithFormat:@"Foo(%@)",delegate];
}
@end
int main() {
id p=[NSAutoreleasePool new];
Foo *foo=[Foo new];
id delegate=@"delegate";
foo->delegate=delegate;
// normally, if archiving just foo, you don't want the delegate archived:
NSLog(@"%@",[NSUnarchiver unarchiveObjectWith
Data:[NSArchiver
archivedDataWithRootObject:foo]]);
// on the other hand, if delegate happens to be archived anyway, you
want to keep the connexion:
NSLog(@"%@",[[NSUnarchiver unarchiveObjectWith
Data:[NSArchiver
archivedDataWithRootObject:[NSArray arrayWithObjects:foo,delegate,nil]]]
objectAtIndex:0]);
[p release];
return 0;
}
18 /tmp> cc -framework Foundation q.m && ./a.out
2002-08-02 17:36:30.816 a.out[477] Foo((null))
2002-08-02 17:36:30.830 a.out[477] Foo(delegate)
19 /tmp>
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.