如何将 AppDelegate 中的 RootViewController 小写以在 Objective C 中定义 NSPersistentContainer

how to downcase rootviewcontroller from appdelegate to define NSPersistentContainer in Objective C

提问人:reactor 提问时间:4/8/2023 最后编辑:The Dreams Windreactor 更新时间:4/11/2023 访问量:69

问:

我正在尝试按照设置 Core Data Stack 文档的步骤使用 Objective-C 设置 Core Data,但它似乎只显示了 Swift 实现。我拥有的主视图控制器是类,它嵌入在导航控制器中,如下所示:TodoListTableViewController

应用程序入口点->UINavigationController->TodoListTableViewController

我试着这样定义我的喜欢:-[UIApplicationDelegate application:didFinishLaunchingWithOptions:]

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if (self.persistentContainer == nil) {
        [NSException raise:@"did not initialize persistent container in app delegate" format:@"no init"];
    }

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    TodoListTableViewController *todoListVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"TodoListTableViewController"];
    todoListVC.persistentContainer = self.persistentContainer;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:todoListVC];
    self.window.rootViewController = navController;
    
    return YES;
}

但是我得到了一个缺少容器异常,因为我添加了对类中存在的检查,如下所示:persistentContainerTodoListTableViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   if (self.persistentContainer == nil) {
     [NSException raise:@"missing container" format:@"missing container"];
   }
}

编辑:下面是我的类中与Ccore Data相关的样板代码,用于在选择加入Core Data时添加Xcode生成的持久容器和上下文,我正在使用:AppDelegate

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"ToDoList"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

以及 AppDelegate 中 xcode 包含的头文件:

@property (readonly, strong) NSPersistentContainer *persistentContainer;

谢谢。

iOS Objective-C 核心数据

评论

0赞 HangarRash 4/8/2023
您是否在拨打线路时检查过您的应用程序委托是否为非 nil?您是否验证过之前正在调用该行?persistentContainertodoListVC.persistentContainer = self.persistentContainer;viewDidLoad
0赞 The Dreams Wind 4/8/2023
如何在应用程序委托类中定义属性?persistentContainer
0赞 reactor 4/10/2023
@HangarRash谢谢。我没有,即使在添加检查后,我也会遇到同样的错误。我将更新代码以包含 nil 检查
0赞 reactor 4/10/2023
@TheDreamsWind我将更新 persistentContainer 属性的代码,因为实现很长
0赞 The Dreams Wind 4/11/2023
@reactor我无法发现任何可能导致异常抛出您提供的代码的内容。特别是因为你已经检查了班级。无论出于何种原因,您都应该释放该类(前提是您的控制器不保留指向持久容器的指针),或者更有可能 - 您在某处分配了另一个实例,该实例尚未设置。例如,故事板可以实例化 a 作为设计层次结构的一部分self.persistentContainer == nilAppDelegateAppDelegateTodoListTableViewControllerpersistentContainerTodoListTableViewController

答: 暂无答案