Re: accessing data within a class
Re: accessing data within a class
- Subject: Re: accessing data within a class
- From: Brent Gulanowski <email@hidden>
- Date: Fri, 25 Jul 2003 20:00:56 -0400
On Friday, July 25, 2003, at 03:47 PM, April Gendill wrote:
I am so lost I don't know what to do....
let me try and explain and see if maybe you can help.
ok in my document controller I have a method that populates an array.
its an NSMutableArray and is declared in the header
interface Mydocument {
NSMutableArray *generatedNumbers;
<more stuff>
}
OK.
I have a function to generated random numbers and it begains
-(void)generateRandomNumbers:(id)sender{
int comparitor;
generatedNumbers=[[NSMutableArray alloc] init];
NSString *swapperString;
NSString *swapperOne;
NSString *swapperTwo;
<code to make a bunch of numbers>
<for loop that runs a specific number of times
and each itteration adds a number to the NSMutableArray
generatedNumbers
by calling a method named "populate array"
it basically looks like:
[populateArray:generated];>
<after that it continues the loop>
after the loop it places the contents of the array into a TableView
item.
It isn't clear what populateArray is needed for, but we'll leave that
for now.
The generatedNumbers array I thought was declared protected so the
whole class could use it but in my save routine:
- (void)saveFile:(id)sender
{
NSSavePanel *panel = [NSSavePanel savePanel];
NSArray *theArray =[self generatedNumbers];
//NSString *theData =[NSString stringWithFormat:@"%@",[myDocument
theSerial]];
NSString *theData =[NSString stringWithFormat:@"%@",[self
generatedNumbers]];
[as an aside, these string variables are just wasted code -- you did it
right in the accessor.]
NSLog(@"the data is:%@",theData);
[panel setRequiredFileType:@"srgn"];
if ([panel runModal] == NSOKButton) {
//[[theData string] writeToFile:[panel filename]
[theData writeToFile:[panel filename]
atomically:YES];
}
}
the NSLog shows me that the generatedNumbers accessor method is
returning null.
and I do have an accessor:
-(NSMutableArray *)generatedNumbers{
NSLog(@"theSerial is returning: %@",generatedNumbers);
return generatedNumbers;
}
All instance methods definitely do have access to all instance
variables, whether they are public, protected, or private -- that's a
central tenet of OOP. If the variables were out of scope, you would get
an error from the compiler. I would have to guess that somewhere you
are (mistakenly?) re-assigning a return value to the generatedNumbers
pointer, and that return value is nil. I'm going to have to guess that
it happens inside your loop, but can't say for sure. You should be
using the debugger to trace the loop and see what happens in there
before it returns. Or that business with the table view? I dunno...
when the accessor prints to the console it is also null. Basically the
instant the generator method finishes and the information shows in the
tableview, the generatedNumbers array, which I thought was going to
stay populated, is purged and all the data is lost.
BUT when i write generatedNumbers to the console at the end of the
random number generator method where it is populated, I get the correct
data.
what am I missing... I know its something simple but I'm nearly in
tears since I have never once gotten an accessor method to return a
value other than nill or null in ANYTHING I have ever tried to write...
I'm really starting to hate cocoa, its feeling like its not worth
learning.
Every new area of knowledge is like that, although granted there are a
few odd things in Cocoa that aren't intuitive at first. But mostly it
follows all the same rules of C and C++ and other languages, especially
when it comes to memory and variables and scope issues -- the
underlying system is the same.
No here is the last part.... IF i place the save code at the end of the
for loop, it brings up a save window and writes the file in plain text
format perfectly.
Thank you.
a very frustraited April.
_______________________________________________
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.
--
Brent Gulanowski email@hidden
_______________________________________________
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.