iphone - 对象在 iOS 5 ARC 中发布得太早

标签 iphone objective-c ios ios5

我是 Objective-C 编程语言的新手,我注意到内存管理中的一个奇怪行为。 在 iOS 5 中,默认情况下启用 ARC,但对象发布得太快,也许有人可以给我一些建议。

是这样的:

我有一个 Dao 对象,它使用 Core Data PersistenceStore 从 SQLite 数据库中检索数据。 这个 Dao 实现了一个方法“getAllAuthors”

在我的 viewWillAppear 方法中的 viewController 中,我加载了这些数据并加载了数据,但是当我填充 tableView 时,对象是(null)。

我发布了一些代码:

@synthesize authors;
@synthesize authorsTable;
@synthesize dao;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"author in viewDidLoad: %@", [[authors objectAtIndex:0] name]);
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    dao = [[CrossoverDao alloc] init];
    authors = [NSArray arrayWithArray:[dao getAllAuthors]];
    [authorsTable reloadData];
    NSLog(@"author in viewWillAppear: %@", [[authors objectAtIndex:0] name]);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [authorsTable reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"author in viewWillDisappear: %@", [[authors objectAtIndex:0] name]);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"author in viewDidDisappear: %@", [[authors objectAtIndex:0] name]);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark -
#pragma mark UITableViewDataSource datasource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath {
    NSLog(@"[AuthorsViewController::cellForRowAtIndexPath] Constructing row at indexPath: %d",     indexPath.row);
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView 
                             dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        if(indexPath.section == 0)
            cell = [[UITableViewCell alloc]     initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
        else
            cell = [[UITableViewCell alloc]     initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [[authors objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = [[authors objectAtIndex:indexPath.row] bio];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
    NSLog(@"author in numberOfRowsInSection: %@", [[authors objectAtIndex:0] name]);
    return [authors count];
}

在 cellForRowAtIndexPath 中,作者为空。有什么线索吗?

提前致谢

最佳答案

你应该确保你使用合成访问器设置你的属性,这样 ARC 知道你想要保留它们,如果它们很强大(这里它只是在你的 viewWillAppear 方法的末尾插入 releases) .变化

dao = [[CrossoverDao alloc] init];
authors = [NSArray arrayWithArray:[dao getAllAuthors]];

self.dao = [[CrossoverDao alloc] init];
self.authors = [NSArray arrayWithArray:[dao getAllAuthors]];

关于iphone - 对象在 iOS 5 ARC 中发布得太早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7846559/

相关文章:

ios - 附加 NSSting 后保留 textView 文本的颜色

ios - xcode 7 中找不到 facebooksdk.h 文件错误

iphone - 从 iPhone 应用程序在同一选项卡中打开 safari

ios - Dlpiechart 仅适用于初始 View Controller

ios - ALAssetsGroup,如何检查是否存在?

ios - 如何正确实现从 iOS 应用程序到 watchOS2 复杂功能的设置传输

ios - 将多台 PC 连接到 Apple 上的同一个 Xamarin Mac Agent

ios - 具有深色效果的圆形 UIButton

iphone - iOs5,试图了解 UIPickerView 以及如何将它连接到我的自定义类

iphone - 如何组合两个可变数组?