Array is empty after calling method

自闭症网瘾萝莉.ら 提交于 2020-02-06 09:21:45

问题


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:


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.



来源:https://stackoverflow.com/questions/3847860/array-is-empty-after-calling-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!