我一直在尝试学习如何在iOS 7上使用CoreData。在使用Data Modeler时,不知何故,Xcode设法创建了一些默认模板方法,如:
//返回应用程序的持久性存储协调器。 //如果 协调员不存在,它被创建和 应用程序的商店添加到它。 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if(_persistentStoreCoordinator!= nil){ return _persistentStoreCoordinator; }
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Myapp_2.sqlite"]; NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
网址:storeURL选项:nil错误:&错误]){ / * 用代码替换此实现以适当地处理错误。
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中为其他项目重新生成模板Core Data函数。
所以我的问题是 - 我们如何强制Xcode 5重新生成这段代码?我没有看到任何明显的东西。
谢谢。
答案 0 :(得分:2)
创建新项目时,应选中“使用核心数据”复选框。
如果要将核心数据添加到现有项目,可以按照本教程进行操作 http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/
答案 1 :(得分:1)
创建一个新项目,例如主 - 细节模板。他们选择“使用核心数据”。