NSCopying & NSCoding Example Code

Fraction


#import <Foundation/Foundation.h>
@class Foo;
@interface Fraction:NSObject<NSCoding, NSCopying>{
	int numerator;//分子
	@public int denominator;//分母
	Foo *foo;
}

-(void)setNumerator:(int)aNumerator;
-(void)setDenominator:(int)aDenominator;

-(int)numerator;
-(int)denominator;

-(void)setFoo:(Foo *)aFoo;
-(Foo *)foo;

@end;

#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Foo.h"

@implementation Fraction

-(void)dealloc{
	NSLog(@"foo retainCount1 : %d", [foo retainCount]);
	[foo release];

	[super dealloc];

}

-(void)setNumerator:(int)aNumerator{
	self->numerator = aNumerator;
}

-(void)setDenominator:(int)aDenominator{
	self->denominator = aDenominator;
}


-(int)numerator{
	return self->numerator;
}
-(int)denominator{
	return self->denominator;
}

-(void)setFoo:(Foo *)aFoo{
	[aFoo retain];
	[foo release];
	foo = aFoo;
	
}
-(Foo *)foo{
	
	return foo;
}


//implementation NSCoding protocol
- (void) encodeWithCoder: (NSCoder*)aCoder{
	[aCoder encodeInt:numerator forKey:@"numerator"];
	[aCoder encodeInt:denominator forKey:@"denominator"];
	[aCoder encodeObject:foo forKey:@"foo"];
}

- (id) initWithCoder: (NSCoder*)aDecoder{
	self = [super init];
	if (self){
		numerator = [aDecoder decodeIntForKey:@"numerator"];
		[self setNumerator:numerator];

		denominator = 	[aDecoder decodeIntForKey:@"denominator"];
		[self setDenominator:denominator];

		[self setFoo:(Foo *)[aDecoder decodeObjectForKey:@"foo"]];
	}
	return self;
}	

//implementation NSCopying protocol
//copyWithZone:(NSZone *)zone,mutableCopyWithZone: (NSZone*)zone 这两个方法生成的对象都是autorelease的
- (id) copyWithZone: (NSZone*)zone{
	//return self;//浅拷贝
	Fraction *aFraction = [[[self class] allocWithZone:zone] init];  
	[aFraction setNumerator:numerator];
	[aFraction setDenominator:denominator];
	[aFraction setFoo:foo];
	return aFraction; 
}


+ (void) load{
	NSLog(@"Auto called the load function.");
}

@end

main


#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Foo.h"


int main (int argc, const char *argv[]) {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	Foo *foo = [[Foo alloc] init];
	
	Fraction *f = [[Fraction alloc] init];
	[f setNumerator:10];
	[f setDenominator:20];
	[f setFoo:foo];
	[foo release];

	NSLog(@"numerator : %d", [f numerator]);
	NSLog(@"denominator : %d", [f denominator]);
	NSLog(@"foo : %@", [f foo]);

	NSData *data = [NSKeyedArchiver archivedDataWithRootObject:f];
	[data writeToFile:@"fraction.db" atomically:NO];
	
	[f release];
	


	/*如果没有实现NSCoding,在序列化的时候不会报错,但是取出的时候结果为空
	 */
	
	Fraction *f1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"fraction.db"];
	NSLog(@"numerator1 : %d", [f1 numerator]);
	NSLog(@"denominator1 : %d", [f1 denominator]);
	NSLog(@"foo1 : %@", [f1 foo]);
	NSLog(@"retainCount1 : %d", [f1 retainCount]);
	
	
	/*
	 *如果没有实现NSCopying,在执行copy方法的时候会直接报错,但是在编译的时候不会出错.
	 *如果是浅拷贝需要在执行copy前,给被copy的对像retain,否则有些obj会造成多次释放
	 *如果是深拷贝则不需要
	 *copyWithZone:(NSZone *)zone,mutableCopyWithZone: (NSZone*)zone 这两个方法生成的对象都是autorelease的
	 */
	
	Fraction *f2 = [f1 copy];
	NSLog(@"numerator2 : %d", [f2 numerator]);
	NSLog(@"denominator2 : %d", [f2 denominator]);
	NSLog(@"foo2 : %@", [f2 foo]);;
	NSLog(@"retainCount2 : %d", [f2 retainCount]);
	[f2 release];
	
	[pool drain];
	return 0;
}



你可能感兴趣的:(Class,interface)