help needed with OO concepts when copying
help needed with OO concepts when copying
- Subject: help needed with OO concepts when copying
- From: Denis Stanton <email@hidden>
- Date: Wed, 12 Feb 2003 08:55:52 +1300
Hi
I hope someone can help me out with a gap in my understanding of object
concepts. I'm trying to make a copy of an MSMutableArray, so that I
have two arrays. I then try and modify the text contents of one of the
arrays and I see the same changes occur in the other array. This tells
me that rather than making two arrays all I am doing is making two
references to the same one. I have spent many hours trying to figure
out this copying thing, but I'm stumped. How do I make a separate copy
of an array with the same initial values as the original, but otherwise
independent of it?
In my app I have an NSMutableArray named reports.
NSMutableArray *reports;
int reportIndex;
reports = [[NSMutableArray alloc] init];
Each row of this array defines the format for a report my app will
produce. The rows are defined by the Report class.
A Report instance contains three items, a report name, report title and
an array defining the items to appear in the report. This array is
called blocks and contains instances of Block.
@interface Report : NSObject <NSCoding> {
NSString *name;
NSString *title;
NSMutableArray *blocks;
}
- (NSString *)name;
- (void)setName: (NSString *)value;
- (NSString *)title;
- (void)setTitle: (NSString *)value;
- (NSMutableArray *)blocks;
- (void)setBlocks: (NSMutableArray *)newBlocks;
@end
@implementation Report
- (void)setBlocks: (NSMutableArray *) newBlocks {
[blocks autorelease];
NSMutableArray *myBlocks = [[NSMutableArray alloc] init];
myBlocks = [newBlocks copy];
blocks = myBlocks;
}
The problem comes when I want to define a new report. The chances are
that a new report will resemble an existing one, with some changes, so
I thought the best thing to offer the user is a duplicate button which
adds another row to the reports array, copying a selected row. The new
row could then be modified. This works fine for the name and title
fields, but not for the blocks array. When I think I'm making a copy of
the blocks array I am actually just making more references to it.
- (void)tableViewSelectionDidChange: (NSNotification *)aNotification {
reportIndex = [tableView selectedRow];
}
- (IBAction)duplicateReport:(id)sender
{
Report *report = [[Report alloc] init];
[report setName: [[reports objectAtIndex: reportIndex] name]];
[report setTitle: [[reports objectAtIndex: reportIndex] title]];
[reports setBlocks : [[reports objectAtIndex: reportIndex] blocks]];
[reports insertObject: report atIndex: reportIndex];
[tableView reloadData];
}
I thought the requirement was to set the blocks array in the new report
row to be a copy of the blocks array in the existing report row, but
this doesn;' seem to help.
Can anybody see what I'm missing here?
I tried adding a copy request, but that doesn't seem to make any
difference
[reports setBlocks : [[[reports objectAtIndex: reportIndex] blocks]
copy]];
Thanks in advance for any advice.
Denis Stanton
_______________________________________________
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.