NSMutableArray

Discussion in 'Mac Programming' started by apffal, Feb 16, 2008.

  1. macrumors newbie

    Joined:
    Feb 10, 2008
    #1
    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.

     
  2. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #2
    Hi there,
    You're allocating a new array each time so it will only ever have the last value.

    b e n
     
  3. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #3
    So, how to solve this ? Adding values to a list ?
    Does tableview can do that task ?
     
  4. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    apfall, you need to instantiate 'array' once somewhere outside of your method. Perhaps make it a member variable, or global.

    b e n
     
  5. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #5
    And how to do it, in this case ?
     
  6. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    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.
     
  7. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #7
    It works fine, thank you !
    A last question : how to view array contents in a tableview ?
     
  8. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    Read Using a Table Data Source or you can use bindings.
     
  9. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #9
    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]; 
    
    } 
    
    
     
  10. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
    Is listBox valid? Try using NSLog() to figure out what's not getting called, etc.
     
  11. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #11
    NSLog gives me a null result for listBox.
    What does that means ?
     
  12. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #12
    Fixed the null result : I've missed to connect listBox to controller.
    But still have no display on it.
     
  13. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #13
    Make sure you are calling reloadData on your tableview whenever you make changes to your array.
     
  14. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #14
    Everything solved : it was that !
    Thanks, again.
     
  15. macrumors newbie

    Joined:
    Sep 1, 2008
    #15
    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
     

Share This Page