【问题标题】:NSArray BAD ACCESSNSArray 错误访问
【发布时间】:2013-02-19 23:11:11
【问题描述】:

我正在尝试做一个pickerView,但我的访问权限很差:

这是我的代码

-(void) viewWillAppear:(BOOL)animated {
    list = [[NSArray alloc]init];
    [self populateList]
}

-(void) populateList {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"nameoffile" ofType:@"txt"];
    NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    list = [file componentsSeparatedByString:@"\n"];
}


 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
     return (NSString *)[list objectAtIndex:row]; //here I'm getting bad acces
 }

错误是:“线程1:EXC_BAD_ACCESS(code=1, address=0xa001cc65)”

【问题讨论】:

  • 请发布崩溃的(符号)堆栈跟踪。
  • OK 在出现错误的行之前添加:NSAssert(row < [list count], @"Row %d is out-of-range", row); 并重新运行您的应用。
  • @trojanfoe 帮不上什么忙。这是一个段错误,而不是 NSException。
  • @H2CO3 是的,但您应该能够使用 bt 从 lldb 获取堆栈跟踪?
  • @illDev: 你确定 pickerView:titleForRow 只有在列表被填充后才会被调用吗?

标签: objective-c ios4


【解决方案1】:

componentsSeparatedByString: 返回的NSArray 是自动释放的值,所以你需要保留它。

你应该删除:

list = [[NSArray alloc]init];

并将保留添加到:

list = [[file componentsSeparatedByString:@"\n"] retain];

【讨论】:

  • 那么他应该使用strong 属性。
  • 你怎么知道他不是?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多