我有一个显示联系人列表的数据表。当我启动应用程序时,所有数据都已正确加载。但是在选择联系人后,我有时会遇到此异常: -
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];
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];
如果我替换这些代码行
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];
// 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];
抛出EXC_BAD_ACESS。
我调试了代码,列表}
包含对象。任何人都可以告诉我可能的原因吗?
答案 0 :(得分:1)
我想你想为“objc_exception_throw”设置一个符号断点,这样你就可以看到崩溃前会发生什么。只需转到“运行”菜单 - >管理断点 - >添加符号断点,并键入objc_exception_throw。
BTW,您可能正在调用已删除的对象。答案 1 :(得分:0)
EXC_BAD_ACCESS上的调用堆栈是什么?
另外,你可以使用NSString来获取格式化的字符串,不需要使用NSMutableString或者转换它......
答案 2 :(得分: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