gpt4 book ai didi

IOS CoreData - 无法加载 NSPersistentStoreCoordinator

转载 作者:行者123 更新时间:2023-11-29 05:05:08 28 4
gpt4 key购买 nike

我有一个名为 TestProject 的 xcode4 项目,我正在尝试将 CoreData 添加到其中。我添加了一个包含一些实体的数据模型(名为 TestDataModel),并为这些实体创建了 NSManagedObject 类。

我的问题是我无法加载持久存储

if(self.managedObjectContext == nil){
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL* storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingPathComponent: @"TestProject.sqlite"]];
NSError* errors;

NSPersistentStoreCoordinator* persistentStoreCoordinator =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel
mergedModelFromBundles:nil ]];

if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:nil
error:&errors])
{
NSLog(@"Error loading persistent store: %@", [errors localizedDescription]);
}

self.managedObjectContext = [[NSManagedObjectContext alloc] init];
[self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

}

我不断收到错误:

Error loading persistant store: The operation couldn’t be completed. (Cocoa error 258.)

请注意,我也在模拟器中运行它。

提前致谢。

最佳答案

您使用了错误的目录。

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
^^^^^^^^^^^^^^^^^^^^^^^^

将其更改为

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
^^^^^^^^^^^^^^^^^^^

NSDocumentDirectory 是由 iOS 创建的,但 NSDocumentationDirectory 目录不是。

关于IOS CoreData - 无法加载 NSPersistentStoreCoordinator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5686115/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com