Re: help with NSCopying
Re: help with NSCopying
- Subject: Re: help with NSCopying
- From: Greg Anderson <email@hidden>
- Date: Sat, 11 Jan 2003 10:43:59 -0600
On Saturday, January 11, 2003, at 05:41 AM, Ted Lowery wrote:
How do I implement -copy for an object?
I have an object:
@interface ObjColorScheme : NSObject <NSCopying> {
NSColor* lineColor;
NSColor* fillColor;
BOOL showLine;
BOOL showFill;
}
then I provide a method in ObjColorScheme:
- (id)copyWithZone:(NSZone*)zone {
// what goes here?
}
I have a class of data for a day object that implements -copy, and I
set it up like this:
- (id)initWithDOM:(int)inDay dataArray:(NSMutableArray *)inArray
{
self = [super init];
if (self) {
iDayOfMonth = inDay;
iDataArray = [inArray copy];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
DayData *copy = [[[self class] allocWithZone: zone]
initWithDOM:iDayOfMonth dataArray:iDataArray];
return copy;
}
-copyWithZone just calls my secondary init function that takes initial
values, and then returns the copy. As you might expect, the DayData
class looks like:
@interface DayData : NSObject {
int iDayOfMonth;
NSMutableArray *iDataArray;
}
Substitute your favorite member variables and have at it.
Greg
---
Greg T. Anderson
Monkey Wrangler
http://homepage.mac.com/torgo
_______________________________________________
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.