问题
My application was working fine, and a couple minutes later, after editing another part of the app, it started throwing the EXC_BAD_ACCESS error from a view controller that I wasn't even working on. The only thing I did between that I can think that may have affected it is, I Edit->Refractor->Convert to Obj-C ARC... I only did it it for a specified file also, not my whole project which is confusing. I was following this tutorial when I believe the error started happening... http://sonnyparlin.com/2011/12/pulltorefresh-ios-5-and-arc-tutorial/ Also, the app launches fine, it just crashes when a cell is clicked on in the tableview. Any help would be greatly appreciated!
Error happens on this line:
self.releaselink = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"link"];
Here's my code where the error is originating from:
@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
Header file"
@interface PressReleaseViewController : UITableViewController {
IBOutlet UITableView *pressReleases;
NSArray *array;
NSArray *sortedArray;
NSString *releaselink;
}
@property (nonatomic, retain) NSString *releaselink;
@end
回答1:
EXC_BAD_ACCESS
errors occur when your application attempts to access an object that has already been released. If the issue happened after you converted the project to ARC, then somewhere there is a Zealous release call being made (on behalf of ARC). Try making sortedArray
a strong property of your class, and set the variable as :
self.sortedArray = [array sortUsingDescriptor:...];
That must be it, because sortUsingDescriptor:
probably returns an autoreleased object. In a non-arc project you'd have to encapsulate that call with a retain call:
[[array sortUsingDescriptor:...]retain];
来源:https://stackoverflow.com/questions/12324950/ios-exc-bad-access-error