Re: Simple Messaging/NSMutableArray question
Re: Simple Messaging/NSMutableArray question
- Subject: Re: Simple Messaging/NSMutableArray question
- From: Ondra Cada <email@hidden>
- Date: Tue, 22 Oct 2002 23:37:11 +0200
On Tuesday, October 22, 2002, at 10:21 , Paul Naro wrote:
This is most likely a simple answer, but I haven't been able to find
it! I have a window with 2 NSTableViews. There are 2 classes that
when a button is clicked, it adds some text to the appropriate array
and does a reloadData on the tableView. Everything is fine at this
point. I call object 1 from object 2 like this ...
I am not entirely sure whether I understand that all, but...
- (IBAction)copyRecord:(id)sender {
[self addToArray];
TestArray1 *aCopy = [[TestArray1 alloc] init];
[aCopy addToArray:@"This is a copied String"];
[aCopy release];
}
...this looks weird. First you send yourself a message "addToArray" --
what's that? There is only "addToArray:" method shown?
Then, you create an object aCopy, add something to its array, and
immediately trash the object (presumably including the array it contained)
!
Incidentally, the pattern
id o=[[Class alloc] init];
dosomethinghere;
[o release];
is WRONG! (Sigh I feel quite tired harping on that every week or so :( ).
The right pattern is
id o=[[[Class alloc] init] autorelease];
dosomethinghere;
for a number of reasons, probably the most important of which is the code
robustness and maintainability. It also does not leak, whilst your code
might.
It passes the string to object A (I can print it out), but doesn't get
added to the array.
Here you lost me completely. To which array?
- (void)addToArray:(NSString *)theString {
...
[array1 addObject:theString];
currentIndex = [array1 count] - 1 ;
[arrayTable1 reloadData];
}
Self-evidently (also from the init method which I did not quote) you first
add the string to the array inside the newly created object, then send
reloadData to nil (which of course does nothing), and then release the
object including the array it contains. What should it be good for?
All I want to do is send a string from one object
and have it populate an array in another object.
That you do, but immediately after that you trash the "another object"
(probably -- if you have your -dealloc implemented right -- including the
array; otherwise the array leaks), so that the net effect is naught.
---
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.