程序接收信号:EXC_BAD_ACCESS

时间:2011-07-07 19:51:24

标签: ios core-data uitableview

我的UITableViewController子类中有以下方法:

-(void)populateDataStorage{

NSString *path = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"];

if(path){
    NSArray *plistData = [[NSArray alloc] initWithContentsOfFile:path];
    NSEnumerator *enumerator = [plistData objectEnumerator];

    NSArray *personResults;

    Photo *photo;
    Person *person;

    id currItem = [enumerator nextObject];

    while (currItem != nil) {
        photo = (Photo *)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext: [[FlickrFetcher sharedInstance] managedObjectContext]];

        photo.name = [currItem objectForKey:@"name"];
        photo.path = [currItem objectForKey:@"path"];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", [currItem objectForKey:@"user"]];

        personResults = [[FlickrFetcher sharedInstance] fetchManagedObjectsForEntity:@"Person" withPredicate:predicate];

        if ([personResults count] > 0) {
            person = [personResults objectAtIndex:0];
        } 
        else {
            person = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[[FlickrFetcher sharedInstance] managedObjectContext]];
            person.name = [currItem objectForKey:@"user"];
        }

        photo.person = person;
        [person addPhotosObject:photo];

        NSLog(@"Photo %@ added for user %@", photo.name, person.name);

        currItem = [enumerator nextObject];
    }

    [plistData release];
}

}

我在AppDelegate中的应用程序didFinishLaunchingWithOptions方法中调用它,该方法也在同一个AppDelegate中,当它被调用时我得到它在调试时所说的错误,如果我不调试它将运行它方法和负载没有问题。如果我根本不调试它将不会调用该方法。

编辑:根据答案更改代码问题仍然保持不变,如果我只是运行没有任何反应,但在调试时我得到了错误。当我调试整个方法时,没有显示错误。

1 个答案:

答案 0 :(得分:3)

您对NSLog的来电正在尝试访问name变量的person属性。但是,当您声明person变量时,您没有对其进行初始化,因此它指向垃圾。您只在else子句中为其指定了有效值,因此有时您的NSLog正在访问未初始化的对象。