【问题标题】:-[UITableViewRowData isEqualToString:]: unrecognized selector sent to instance 0x391dce0-[UITableViewRowData isEqualToString:]:无法识别的选择器发送到实例 0x391dce0
【发布时间】:2010-04-28 21:07:09
【问题描述】:

我有一个显示联系人列表的数据表。当我启动应用程序时,所有数据都已正确加载。但选择联系人后,有时会出现此异常:-

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 = 名称; 返回单元格; }

如果我替换这些代码行

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_ACES。 我调试了代码,列表appDelegate expensivePersonsList 包含对象。谁能告诉我可能的原因?

【问题讨论】:

  • 我想我找到了问题,问题出在 person 对象上,它以某种方式被释放了。但是这怎么会发生,我将这个对象传递给一个视图控制器链。 @property (nonatomic, retain) IBOutlet Person *selectedPerson; 而且我不会释放这个对象的任何视图控制器。

标签: iphone


【解决方案1】:

我认为你想为“objc_exception_throw”设置一个符号断点,这样你就可以看到崩溃前发生了什么。只需转到运行菜单 -> 管理断点 -> 添加符号断点,然后输入 objc_exception_throw。

顺便说一句,您可能正在调用一个已被删除的对象。

【讨论】:

    【解决方案2】:

    EXC_BAD_ACCESS 上的调用堆栈是什么?

    另外,您可以只使用 NSString 来获取格式化字符串,无需使用 NSMutableString 或强制转换...

    【讨论】:

    • 对于 EXC_BAD_ACCESS,控制台上没有堆栈跟踪。它只是退出说“程序收到信号:”EXC_BAD_ACCESS”。关于演员表和 NSMutableString,你是对的,我可以删除它们。但删除它们无助于解决问题。
    【解决方案3】:

    最后,我发现了问题所在。实际上,我想做的是将 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

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2011-10-16
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2012-07-24
      相关资源
      最近更新 更多