【发布时间】:2014-05-06 15:08:15
【问题描述】:
我一直在尝试学习如何在 iOS 7 上使用 CoreData。在使用 Data Modeler 时,Xcode 设法创建了一些默认模板方法,例如:
// 返回应用程序的持久存储协调器。 // 如果 协调器不存在,它被创建并且 应用程序的商店添加到它。 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { 返回_persistentStoreCoordinator; }
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Myapp_2.sqlite"]; NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nilURL:storeURL 选项:nil 错误:&error]) { /* 将此实现替换为代码以适当地处理错误。
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application,虽然它可能在开发过程中很有用。
Typical reasons for an error here include: * The persistent store is not accessible; * The schema for the persistent store is incompatible with current managed object model. Check the error message to determine what the actual problem was. If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into应用程序的资源目录而不是可写目录 目录。
If you encounter schema incompatibility errors during development, you can reduce their frequency by: * Simply deleting the existing store: [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] * Performing automatic lightweight migration by passing the following dictionary as the options parameter: @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration编程指南”了解详情。
*/ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _persistentStoreCoordinator; }
我不知道它是如何生成这个明显非常有用的起点的,而且我无法强制 Xcode 在 AppDelegate 中为其他项目重新生成模板核心数据函数。
所以我的问题是 - 我们如何强制 Xcode 5 重新生成此代码?我没有看到任何明显的东西。
谢谢。
【问题讨论】:
-
我猜你必须创建一个新项目并从那里复制这些方法......
-
我不需要做任何类似的事情,它会自动在应用程序委托中生成代码,甚至将内容添加到我的应用程序委托的 .h 文件中......
-
是的,当您创建一个使用 CoreData 选中的新项目时确实如此
-
哦,我现在看到了。我想,没有办法强制它重新创建任何东西,因为它是在项目创建时创建的。这不是可以一遍又一遍地重新创建的东西..