3行代码,一行导致EXC_BAD_ACCESS

时间:2012-03-16 18:16:07

标签: ios

我有一个数组,我想用字典填充,但是当我尝试查看填充了数组的pickerView时,我得到了EXC_BAD_ACCESS。这3行代码中的一行导致了它。

paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) copy];
  NSString *documentsDirectory = [paths objectAtIndex:0]; 
  NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"fullArray.plist"];

完整代码

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"fullArray.plist"];

dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
array = [dictionary allKeys];

[pickerView selectRow:0 inComponent:0 animated:YES];
[pickerView reloadAllComponents];

}

2 个答案:

答案 0 :(得分:0)

也许NSSearchPathForDirectoriesInDomains返回的NSArray正在变成僵尸。您可以尝试获取它的副本:

NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) copy];

答案 1 :(得分:0)

看起来,通过这一行,您正在尝试将值保存到名为“数组”的ivar中:

array = [dictionary allKeys];

但这不会起作用(除非它和ARC项目,你说它不是)。

您需要保留该值。使数组成为保留(或复制)的属性。或者(不太好)自己保留它:

[array autorelease];  // Don't skip this, or it may leak.
array = [[dictionary allKeys] retain];