我在使用發佈配置運行時遇到錯誤,這似乎是本地變量tmp
的過早發佈。本地變量的ARC生命週期,過早發佈
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'
相關代碼:
@property (nonatomic, strong) NSIndexPath *selectedCellIndexPath;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_selectedCellIndexPath != nil && [_selectedCellIndexPath isEqual:indexPath]) {
self.selectedCellIndexPath = nil;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (_selectedCellIndexPath != nil && ![_selectedCellIndexPath isEqual:indexPath]) {
//--- problematic code
NSIndexPath *tmp = _selectedCellIndexPath;
self.selectedCellIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:@[tmp, _selectedCellIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//--- problematic code
} else {
self.selectedCellIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
我有一個印象,局部變量tmp
應該在這裏有很強的借鑑意義或者我不好嗎?
順便說一句,更改代碼到
NSIndexPath *tmp = self.selectedCellIndexPath;
或改變
@[tmp, _selectedCellIndexPath]
到[NSArray arrayWithObjects:tmp,_selectedCellIndexPath,nil]
解決問題。
這是什麼問題?
我不知道。也許self.selectedCellIndexPath創建一個副本?區別在於self.selectedCellIndexPath會像[self selectedCellIndexPath]那樣調用getter方法。訪問_selectedIndexPath意味着使用或更改iVar本身,而不給予ARC正確管理內存的機會。一般來說,如果沒有getter或setter本身,你應該更喜歡使用setter/getters,而不是直接訪問iVar – 2013-03-03 15:18:20