调用方法后数组为空 [英] Array is empty after calling method

查看:136
本文介绍了调用方法后数组为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我调用方法 - (void)setArrayCheckOut:(int)num 在另一个类数组 arrayCheckout 是空的。在之后调用 - (IBAction)reloadTable:(id)sender - (void)setArrayCheckOut:(int)num results in reload table - (null),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".

有什么问题吗?

这么长

@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


推荐答案

如果数组为空, objectAtIndex:0 将抛出异常。

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

nil ,您没有数组:您已将 objectAtIndex:消息发送到 nil

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

很可能, CheckOut 不是类的实例由 init 初始化。检查其超类的文档以查看其指定的初始化程序是什么,然后覆盖它。

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.

这篇关于调用方法后数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆