2010-04-28 64 views
1

我有一個數據表,顯示聯繫人列表。當我開始選擇聯繫人後,所有的數據被加載correctly.But的應用,我有時會得到這個異常: -- [UITableViewRowData isEqualToString:]:無法識別的選擇發送到實例0x391dce0

Program received signal: 「EXC_BAD_ACCESS」. 有時

-[UITableViewRowData isEqualToString:]: unrecognized selector sent to instance 0x391dce0

最可能就是因爲這個代碼: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

} 
ExpenseTrackerAppDelegate *appDelegate = (ExpenseTrackerAppDelegate *)[[UIApplication sharedApplication] delegate]; 

Person *person = (Person *)[appDelegate.expensivePersonsList objectAtIndex:indexPath.row];

NSString *name = [NSString stringWithFormat:@"%@ %@" ,person.lastName , person.firstName];

cell.textLabel.text = name; return cell; }

如果我更換這行代碼

NSString *name = [NSString stringWithFormat:@"%@ %@" ,person.lastName , person.firstName]; cell.textLabel.text = name;

與此代碼 cell.textLabel.text = person.lastName;

然後一切工作正常?

我不知道究竟發生了什麼? 經過查看和調試後,我發現這段代碼似乎不起作用。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]]; 
PersonnalDetails *detailViewController = [[PersonnalDetails alloc] initWithNibName:@"PersonnalDetails" bundle:[NSBundle mainBundle]]; 
//detailViewController.view = [[UITableView alloc] initWithNibName:@"PersonnalDetails" bundle:[NSBundle mainBundle]]; 
// ... 
// Pass the selected object to the new view controller. 
    ExpenseTrackerAppDelegate *appDelegate = (ExpenseTrackerAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSInteger row = indexPath.row; 
Person *person = [[appDelegate expensivePersonsList] objectAtIndex:row]; 
NSLog(@"%@", person.firstName); 
    detailViewController.selectedPerson = person; 
[self.navigationController pushViewController:detailViewController animated:YES]; 
[detailViewController release]; 

} 這一行 NSLog(@"%@", person.firstName); 拋出EXC_BAD_ACESS。 我調試了代碼,列表appDelegate expensivePersonsList包含對象。有人可以告訴我可能的原因嗎?

+0

我想我找到了問題,問題是人的對象,它就會以某種方式釋放。但是,怎麼會發生這種情況,我將這個對象傳遞給視圖控制器鏈。 @property (nonatomic, retain) IBOutlet Person *selectedPerson; 而我不會釋放這個對象的任何視圖控制器。對於EXC_BAD_ACCESS, – awsome 2010-04-28 23:13:03

回答

0

最後我發現了這個問題。實際上,我想要做的是將Person對象的所有屬性設置爲只讀,以便只有在init方法中,屬性纔會被初始化。因此Person.h如下所示:

@interface Person : NSObject { 
     NSInteger pid; 
     NSString const *firstName; 
     NSString const *lastName; 
     NSString const *phoneNumber; 
     NSString const *emailAddress; 
} 

@property (readonly) NSString *firstName; 
@property (readonly) NSString *lastName; 
@property (readonly) NSString *phoneNumber; 
@property (readonly) NSString *emailAddress; 
@property (readonly) NSInteger pid;  

-(id)initWithName:(NSString *)n lastName:(NSString *)l phoneNumber:(NSString *)p emailAddress:(NSString *)e pid:(NSInteger)i; 
@end 

並且所有這些值都沒有保留這些值。

這個問題是我的所有錯誤的主要問題: Initializing a readonly property

0

EXC_BAD_ACCESS上的調用堆棧是什麼?

另外,你可以使用NSString得到一個格式化字符串,沒有必要使用的NSMutableString或將其丟...

+0

,控制檯上沒有堆棧跟蹤。它只是退出說:「編程接收到的信號:」EXC_BAD_ACCESS「 以及關於演員和NSMutableString你是對的,我可以刪除它們,但刪除它們無助於解決問題。 – awsome 2010-04-28 21:18:06

1

我想你想提出一個象徵性的斷點「objc_exception_throw」所以你可以看到事故發生前會發生什麼。只需進入Run菜單 - >管理斷點 - >添加符號斷點,然後輸入objc_exception_throw。

順便說一句,你可能正在調用一個已經被刪除的對象。

相關問題