1

我的应用程序运行良好,几分钟后,在编辑了应用程序的另一部分后,它开始从我什至没有处理的视图控制器中抛出 EXC_BAD_ACCESS 错误。我认为可能影响它的唯一一件事是,我编辑->折射->转换为 Obj-C ARC...我也只为指定的文件做了它,而不是我的整个项目令人困惑。当我相信错误开始发生时,我正在关注本教程...... http://sonnyparlin.com/2011/12/pulltorefresh-ios-5-and-arc-tutorial/ 此外,应用程序启动正常,它只是在崩溃时崩溃在表格视图中单击了一个单元格。任何帮助将不胜感激!

此行发生错误:

self.releaselink = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"link"];

这是我的错误源自的代码:

@implementation PressReleaseViewController

@synthesize releaselink;

UILabel *departmentNamesLabel;
CGRect *departmentNamesFrame;

- (void)viewDidLoad {

// Load path to plist and sort based on "name"

NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"departments" ofType:@"plist"];
array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
}

// How many sections are in the table

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

// Set how many rows of cells are in the table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [sortedArray count];
}

// Set up table view and format for cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

// Set text for each cell

cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"name"];

return cell;
}

// Set how tall each cell is

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}

// Set push to new view controller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

self.releaselink = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"link"];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
MainViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"main"];
[vc setReleaseLink:self.releaselink];
[self.navigationController pushViewController:vc animated:YES];

}

- (void)viewDidUnload {
pressReleases = nil;
[super viewDidUnload];
}
@end

头文件”

@interface PressReleaseViewController : UITableViewController {

IBOutlet UITableView *pressReleases;
NSArray *array;
NSArray *sortedArray;
NSString *releaselink;

}

@property (nonatomic, retain) NSString *releaselink;

@end
4

1 回答 1

12

EXC_BAD_ACCESS当您的应用程序尝试访问已释放的对象时会发生错误。如果问题是在您将项目转换为 ARC 之后发生的,那么某个地方会发出 Zealous 发布调用(代表 ARC)。尝试为你的类创建sortedArray一个强大的属性,并将变量设置为:

self.sortedArray = [array sortUsingDescriptor:...];

那一定是它,因为sortUsingDescriptor:可能返回一个自动释放的对象。在非弧项目中,您必须使用保留调用封装该调用:

[[array sortUsingDescriptor:...]retain];
于 2012-09-07T20:42:19.660 回答