0

我正在尝试做一个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)”

4

1 回答 1

2

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

您应该删除:

list = [[NSArray alloc]init];

并将保留添加到:

list = [[file componentsSeparatedByString:@"\n"] retain];
于 2013-02-19T15:10:16.380 回答