views:

72

answers:

2

Hi,

everytime I call the method -(void)setArrayCheckOut:(int)num in another class the array arrayCheckout is empty. Calling -(IBAction)reloadTable:(id)sender after -(void)setArrayCheckOut:(int)num "results in reload table - (null), 0".

Any idea what goes wrong?

so long

@implementation CheckOut
-(id)init
{
[super init];
tableCheckOut = [[NSTableView alloc]init];
if (!arrayCheckOut)
{
arrayCheckOut = [[NSMutableArray alloc]init];
[arrayCheckOut addObject:@"-"];
}

return self;
     }
-(void)setArrayCheckOut:(int)num
{

  switch (num) {
 case 170:
 [arrayCheckOut addObject:@"T20, T20, DB"];
 break;
 default:
 [arrayCheckOut addObject:@"-"];
 break;
   }
 NSLog(@"array = %@",[arrayCheckOut objectAtIndex:0]);

[tableCheckOut reloadData];

}


-(IBAction)reloadTable:(id)sender
{
NSLog(@"reload table - %@, %d",[arrayCheckOut objectAtIndex:0],[arrayCheckOut count]);

[tableCheckOut reloadData];
}


- (int)numberOfRowsInTableView:(NSTableView *)tv
{
return [arrayCheckOut count];
}

- (id)tableView:(NSTableView *)tv
objectValueForTableColumn:(NSTableColumn *)tColumn
 row:(int)row
{
NSString *v = [arrayCheckOut objectAtIndex:row];
return v;
}
 @end
+1  A: 

If the array were empty, objectAtIndex:0 would throw an exception.

Since it doesn't, but returns nil, you don't have an array: You have sent the objectAtIndex: message to nil.

Most probably, CheckOut is not the sort of class whose instances are initialized by init. Check the documentation for its superclass to see what its designated initializer is, then override that instead.

Peter Hosey
I'm new to programming... What kind auf initializer should I use? The superclass is NSObject so I expected there is only one initializer (init).
lueda
You should implement whatever initializer will be called. The only initializer NSObject provides is `init`, so that's probably the one you need to implement in your subclass; since you have implemented it to create the array, that returns us to the question of why you don't have an array. Did you omit from your question some code that sets the `arrayCheckOut` variable back to `nil`?
Peter Hosey
Grepped the content of my project. arrayCheckOut is only defined in CheckOut.h/m. So here's my whole project including all sourcefiles: http://stubbi.org/Darx.zip. You can see, `arrayCheckOut` is correctly set but the table isn't refreshed with the new value. `arrayCheckOut` got its default value again. Perhaps you find the mistake
lueda
Calling `[self setArrayCheckOut:170]` in `-(IBAction)reloadTable:(id)sender` returns the expected result. The array is written and persistent. But why I can't call the method from another class, where I initiliazed an object from CheckOut?
lueda
One problem you have is that you are creating one instance of `Checkout` in the xib and a second one in the `x01` class.Remove the `cOut` initialization from `x01`'s init method, change `cOut` to be an IBOutlet and then connect it to the instance of `Checkout` in MainMenu.xib.
Nathan Kinsinger
A: 

Thank you Nathan. That was the answer!

lueda