0

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
3
  • where is arrayCheckOut defined? Show the code. Commented Oct 2, 2010 at 23:23
  • Nowhere because I don't need it. The array will only read by the application and deleted completly before I fill it up again. Commented Oct 2, 2010 at 23:29
  • Problems with setArrayCheckOut: include a magic number (170), magic and un-localized string literals, use of int where NSInteger or NSUInteger might be more appropriate (also a problem in your data source methods), and the non-descriptive name of the method. I suggest defining constants for the number and strings, localizing the strings using NSLocalizedString (since they seem to be intended for display to the user), switching to NSInteger or NSUInteger, and renaming setArrayCheckOut: to something that more clearly states the method's goal, rather than just its implementation. Commented Oct 2, 2010 at 23:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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).
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?
Grepped the content of my project. arrayCheckOut is only defined in CheckOut.h/m. So here's my whole project including all sourcefiles: 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
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.