PDA

View Full Version : NSMutableArray




apffal
Feb 16, 2008, 03:44 PM
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];
}



lazydog
Feb 16, 2008, 03:51 PM
Hi there,
You're allocating a new array each time so it will only ever have the last value.

b e n

apffal
Feb 16, 2008, 04:17 PM
So, how to solve this ? Adding values to a list ?
Does tableview can do that task ?

lazydog
Feb 16, 2008, 04:29 PM
apfall, you need to instantiate 'array' once somewhere outside of your method. Perhaps make it a member variable, or global.

b e n

apffal
Feb 16, 2008, 05:22 PM
And how to do it, in this case ?

HiRez
Feb 16, 2008, 08:51 PM
Put your array declaration in your interface (.h) file for the class:

NSMutableArray *myArray;

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

- (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:

- (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.

apffal
Feb 17, 2008, 05:24 AM
It works fine, thank you !
A last question : how to view array contents in a tableview ?

kainjow
Feb 17, 2008, 09:03 AM
A last question : how to view array contents in a tableview ?

Read Using a Table Data Source (http://developer.apple.com/documentation/Cocoa/Conceptual/TableView/Tasks/UsingTableDataSource.html) or you can use bindings.

apffal
Feb 17, 2008, 06:18 PM
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.


- (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];

}

kainjow
Feb 17, 2008, 06:26 PM
Is listBox valid? Try using NSLog() to figure out what's not getting called, etc.

apffal
Feb 17, 2008, 07:13 PM
NSLog gives me a null result for listBox.
What does that means ?

apffal
Feb 17, 2008, 07:34 PM
Fixed the null result : I've missed to connect listBox to controller.
But still have no display on it.

kainjow
Feb 17, 2008, 07:51 PM
Make sure you are calling reloadData on your tableview whenever you make changes to your array.

apffal
Feb 17, 2008, 08:10 PM
Everything solved : it was that !
Thanks, again.

faceleg
Sep 1, 2008, 06:44 AM
I've been struggling to get my NSTableView hooked up, this post was the answer!

Coming from Java, I have no understanding of:

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