NSArray BAD ACCESS

时间:2013-02-19 15:06:17

标签: objective-c ios4

我正在尝试做一个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(代码= 1,地址= 0xa001cc65)”

1 个答案:

答案 0 :(得分:2)

NSArray返回的

componentsSeparatedByString:是自动释放的值,因此您需要保留它。

你应该删除:

list = [[NSArray alloc]init];

并将retain添加到:

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