提问人:reactor 提问时间:4/8/2023 最后编辑:The Dreams Windreactor 更新时间:4/11/2023 访问量:69
如何将 AppDelegate 中的 RootViewController 小写以在 Objective C 中定义 NSPersistentContainer
how to downcase rootviewcontroller from appdelegate to define NSPersistentContainer in Objective C
问:
我正在尝试按照设置 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;
}
但是我得到了一个缺少容器异常,因为我添加了对类中存在的检查,如下所示:persistentContainer
TodoListTableViewController
- (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;
谢谢。
答: 暂无答案
评论
persistentContainer
todoListVC.persistentContainer = self.persistentContainer;
viewDidLoad
persistentContainer
self.persistentContainer == nil
AppDelegate
AppDelegate
TodoListTableViewController
persistentContainer
TodoListTableViewController