[英]iOS EXC_BAD_ACCESS error
我的應用程序運行良好,幾分鍾后,在編輯了應用程序的另一部分后,它開始從我什至沒有工作的視圖控制器中引發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
當您的應用程序嘗試訪問已發布的對象時,發生EXC_BAD_ACCESS
錯誤。 如果在將項目轉換為ARC后發生問題,則在某個地方(代表ARC)進行了Zealous發布調用。 嘗試使sortedArray
成為類的強大屬性,並將變量設置為:
self.sortedArray = [array sortUsingDescriptor:...];
一定是這樣,因為sortUsingDescriptor:
可能返回一個自動釋放的對象。 在非弧形項目中,您必須使用“保留”調用封裝該調用:
[[array sortUsingDescriptor:...]retain];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.