Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

apffal

macrumors newbie
Original poster
Feb 10, 2008
24
0
In my application, I make a calculation and want an addition of result values - which I add into an NSMutableArray.
However, when I try it, cannot get the total, but only the last value I've added to the array.
Enabling garbage collection in project settings (I'm working in Leopard) did not solve this problem.
Can someone help ?
That's the relevant code.

- (float)calcJur {
...............
}

- (IBAction)calc:(id)sender {
float juros;
calc = [[Calc alloc]init];
array = [[NSMutableArray alloc]init];
juros = [calc calcJur];
[array addObject:[NSNumber numberWithFloat:juros]];
int itemCount = [array count];
total = 0;
for (k = 0; k < itemCount; ++k)
{
p = [[array objectAtIndex:k] floatValue];
total = total + p;
}
[jurField setFloatValue:total];
}
 
So, how to solve this ? Adding values to a list ?
Does tableview can do that task ?
 
Put your array declaration in your interface (.h) file for the class:

Code:
NSMutableArray *myArray;

then instantiate it in the class's init method, something like:

Code:
- (id)init {
	if (self = [super init]) {
		myArray = [[NSMutableArray alloc] init];
	}
	return self;
}

and of course release it in your dealloc method so you don't leak memory:

Code:
- (void)dealloc {
	[myArray release];
	[super dealloc];
}

You should also wrap your code in "code" (with brackets around it) tags so it gets formatted properly when you post...makes it a lot easier to read.
 
It works fine, thank you !
A last question : how to view array contents in a tableview ?
 
Thank you !
I've dragged a NSTableView to my window, added in header file (IBOutlet NSTableView *listBox), created the data source, got no errors, but my result (the array contents) doesn't appear in that tableview.
What else must I do ?
That's my code.

Code:
- (float)calcJur {
--------
}

- (void)awakeFromNib {

[listBox setDataSource:self];[listBox sizeLastColumnToFit];
}

- (int)numberOfRowsInTableView: (NSTableView *)aTable {

return[results count];
}

- (id)tableView: (NSTableView *)aTable 
objectValueForTableColumn: (NSTableColumn *)aColumn row:(int)rowIndex {
        
return[results objectAtIndex:rowIndex];
}

- (id)init {
if (self = [super init]) {
results = [[NSMutableArray alloc] init];
}
return self;
}

- (void)dealloc {
[results release];
[super dealloc];
}

- (IBAction)calc:(id)sender { 

float juros;
calc = [[Calc alloc]init]; 	
juros = [calc calcJur]; 

[results addObject:[NSNumber numberWithFloat:juros]]; 
int itemCount = [results count];       
total = 0;
for (k = 0; k < itemCount; ++k)
{
pt = [[results objectAtIndex:k] floatValue];
total = total + pt; 
}

[jurField setFloatValue:total]; 

}
 
Is listBox valid? Try using NSLog() to figure out what's not getting called, etc.
 
NSLog gives me a null result for listBox.
What does that means ?
 
Fixed the null result : I've missed to connect listBox to controller.
But still have no display on it.
 
Make sure you are calling reloadData on your tableview whenever you make changes to your array.
 
Thanks!

I've been struggling to get my NSTableView hooked up, this post was the answer!

Coming from Java, I have no understanding of:

Code:
cellsInTable = [[NSMutableArray alloc] init];

and haven't used it elsewhere in my code. The code works, when I learn more about ObjC I'll go through and make it better, sure.

But now it works!

Thanks :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.