函数执行后数组为空
我是 Cocoa Touch 的新手,我有一个问题需要解决。如果有人可以提供帮助,我将不胜感激。
我想创建一个 tableDataList 以显示在表格上。 AsyncViewController会调用TableHandler的fillList方法来初始化表格数据。但是在 fillList 调用之后,tableDataList 返回空。
谁能给我解释一下这个问题吗?
在“AsyncViewController.m”中:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[self.myHandler fillList];
[super viewDidLoad];
}
在“TableHandler.m”中:
@implementation TableHandler
#define ItemNumber 20
@synthesize tableDataList;
- (void) fillList {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:(NSUInteger) 20];
for (NSUInteger i = 0; i < ItemNumber; i++) {
[array addObject:[NSString stringWithFormat:@"Row %d", i]];
}
tableDataList = [NSArray arrayWithArray:array];
}
谢谢
I am newbie with Cocoa Touch, I have a problem that I try to figure out. I will appreciate if anyone could help.
I would like to create a tableDataList to display on the table. AsyncViewController will call TableHandler fillList method to initialize table data. But after fillList call, the tableDataList return empty.
Can anyone explain me this problem?
In "AsyncViewController.m":
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[self.myHandler fillList];
[super viewDidLoad];
}
In "TableHandler.m":
@implementation TableHandler
#define ItemNumber 20
@synthesize tableDataList;
- (void) fillList {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:(NSUInteger) 20];
for (NSUInteger i = 0; i < ItemNumber; i++) {
[array addObject:[NSString stringWithFormat:@"Row %d", i]];
}
tableDataList = [NSArray arrayWithArray:array];
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tableDataList 需要保留新数组,否则它将在调用后很快自动释放。
如果 tableDataList 是带有
retain
的 @property,只需将上面的行更改为:,setter 就会为您处理它。
代码中相当于@property (retain) NSArray *tableDataList;,
上面的代码会在替换变量时自动释放并保留对象,使用
self.tableDataList = SOMETHING.但是,如果您仅使用
tableDataList = SOMETHING
,则您没有使用上述设置器,而是直接设置变量。tableDataList needs to retain the new array, or it will be autoreleased soon after your call.
If tableDataList is a @property with
retain
, just change the line above to:and the setter will handle it for you.
The equivalent of a
@property (retain) NSArray *tableDataList;
is in code,The above code will automatically release and retain objects when you replace the variable, using
self.tableDataList = SOMETHING
. However, if you just usetableDataList = SOMETHING
you are not using the above setter, you're setting the variable directly.你确定它是空的而不是零吗?您可能需要在使用之前分配 tableDataList
Are you sure it's empty and not nil? You may need to alloc the tableDataList before you use it