iOS EXC_BAD_ACCESS错误

时间:2012-09-07 20:29:10

标签: ios exc-bad-access

我的应用程序工作正常,几分钟后,在编辑应用程序的另一部分后,它开始从我甚至没有工作的视图控制器抛出EXC_BAD_ACCESS错误。我之间唯一可以认为可能影响它的是,我编辑 - > Refractor->转换为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

1 个答案:

答案 0 :(得分:10)

当您的应用程序尝试访问已释放的对象时,会发生

EXC_BAD_ACCESS错误。如果在将项目转换为ARC后发生问题,那么就会发生一个Zealous发布调用(代表ARC)。尝试将sortedArray设为您的类的属性,并将变量设置为:

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

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

[[array sortUsingDescriptor:...]retain];