@protocol NSCopying
– (id) copyWithZone: (NSZone *) zone;
@end
– (id) copyWithZone: (NSZone *) zone;
@end
If you adopt NSCopying, your object knows how to make copies of itself:
@interface Vehicle: NSObject<NSCopying>
{
NSColor* color;
}
// methods…
@end
-(id) copyWithZone: (NSZone *)zone {
Vehicle * vehicleCopy = [[[self class] allocWithZone: zone] initWithColor: color];
return vehicleCopy;
}
Vehicle * vehicleCopy = [[[self class] allocWithZone: zone] initWithColor: color];
return vehicleCopy;
}
@interface Car: Vehicle // Car includes the <NSCopying> protocol
{
float drivingForce;
}
// methods…
@end
// Need to implement copyWithZone, though.
// Make sure Car’s drivingForce instance variable is copied.
-(id) copyWithZone: (NSZone *)zone {
Car * vehicleCopy = [super copyWithZone: zone];
[vehicleCopy setDrivingForce: drivingForce];
return vehicleCopy;
}
// Make sure Car’s drivingForce instance variable is copied.
-(id) copyWithZone: (NSZone *)zone {
Car * vehicleCopy = [super copyWithZone: zone];
[vehicleCopy setDrivingForce: drivingForce];
return vehicleCopy;
}
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/
Protocols/NSCopying_Protocol/Reference/Reference.html