函数执行后数组为空

发布于 2024-09-13 20:02:00 字数 871 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-09-20 20:02:00

tableDataList 需要保留新数组,否则它将在调用后很快自动释放。

如果 tableDataList 是带有 retain 的 @property,只需将上面的行更改为:

self.tableDataList = [NSArray arrayWithArray:array];

,setter 就会为您处理它。

代码中相当于@property (retain) NSArray *tableDataList;,

- (void)setTableDataList:(NSArray *)anArray
{
    if (tableDataList != nil) {
        [tableDataList autorelease]; 
    }
    tableDataList = [anArray retain];
}

上面的代码会在替换变量时自动释放并保留对象,使用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:

self.tableDataList = [NSArray arrayWithArray:array];

and the setter will handle it for you.

The equivalent of a @property (retain) NSArray *tableDataList; is in code,

- (void)setTableDataList:(NSArray *)anArray
{
    if (tableDataList != nil) {
        [tableDataList autorelease]; 
    }
    tableDataList = [anArray retain];
}

The above code will automatically release and retain objects when you replace the variable, using self.tableDataList = SOMETHING. However, if you just use tableDataList = SOMETHING you are not using the above setter, you're setting the variable directly.

歌入人心 2024-09-20 20:02:00

你确定它是空的而不是零吗?您可能需要在使用之前分配 tableDataList

Are you sure it's empty and not nil? You may need to alloc the tableDataList before you use it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文