对象为零,除非我添加另一行“随机”代码

时间:2012-09-18 13:00:10

标签: objective-c ios xcode core-data null

我想我可能会发疯。在以下方法中,返回值_persistentStoreCoordinator为nil ,除非我添加另一行代码。检查_persistentStoreCoordinator == nil就足以确保它不是。 (NSLog语句也可以解决问题。)

如果我不添加另一行,_persistentStoreCoordinator在方法的最后一行是nil,即使用断点psc检查它总是非零。

最奇怪的(或者也许是最有帮助的?)事情是,当问题开始时我没有对这个班级做任何改动。

非常感谢任何帮助或解释!

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (_persistentStoreCoordinator == nil) {
        NSLog(@"SQLITE STORE PATH: %@", [self pathToLocalStore]);
        NSURL *storeURL = [NSURL fileURLWithPath:[self pathToLocalStore]];
        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]
                                             initWithManagedObjectModel:[self managedObjectModel]];
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
        NSError *e = nil;
        if (![psc addPersistentStoreWithType:NSSQLiteStoreType
                               configuration:nil
                                         URL:storeURL
                                     options:options
                                       error:&e]) {
            NSDictionary *userInfo = [NSDictionary dictionaryWithObject:e forKey:NSUnderlyingErrorKey];
            NSString *reason = @"Could not create persistent store.";
            NSException *exc = [NSException exceptionWithName:NSInternalInconsistencyException
                                                       reason:reason
                                                     userInfo:userInfo];
            @throw exc;
        }

        _persistentStoreCoordinator = psc;
        if (_persistentStoreCoordinator == nil)
        {
            NSLog(@"We never reach here.");
        }
    }

    return _persistentStoreCoordinator;
}

0 个答案:

没有答案