【问题标题】:Xcode 5 not generating core data template functions in AppDelegateXcode 5 未在 AppDelegate 中生成核心数据模板函数
【发布时间】: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:nil

URL: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 选中的新项目时确实如此
  • 哦,我现在看到了。我想,没有办法强制它重新创建任何东西,因为它是在项目创建时创建的。这不是可以一遍又一遍地重新创建的东西..

标签: ios xcode core-data


【解决方案1】:

当你创建一个新项目时,你应该选中“Use Core Data”复选框。

如果您想将核心数据添加到现有项目中,您可以按照本教程进行操作 http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/

编辑: 您有很多关于此的文章: https://www.google.fr/search?q=add+core+data+to+existing+project+xcode+5&oq=add+coredata+to+&aqs=chrome.1.69i57j0l5.2540j0j7&sourceid=chrome&espv=210&es_sm=91&ie=UTF-8

【讨论】:

  • 我建议您自己将这些方法添加到您现有的 appDelegate 中。这是了解 Core Data 堆栈的构建方式和工作原理的好方法。
【解决方案2】:

创建一个新项目,例如主从模板。他们选择“使用核心数据”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 2011-07-16
    • 2011-07-26
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2016-11-02
    相关资源
    最近更新 更多