gpt4 book ai didi

objective-c - 如何将核心数据添加到现有的基于选项卡的 IOS 项目

转载 作者:行者123 更新时间:2023-11-28 17:36:44 26 4
gpt4 key购买 nike

我尝试了很多不同的答案,但我似乎无法让它发挥作用。我正在尝试将核心数据添加到我现有的基于选项卡的项目中。我通过目标添加了核心数据框架,正确设置了数据模型和实体,但我似乎无法访问它。我收到了许多不同的错误,但最近的错误是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'

我使用预设的核心数据建立了一个新的基于实用程序的项目,并直接复制了代码。我只是将文件 URL 名称更改为当前项目的名称,但它不起作用。这是我的代码:

appDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
BOOL isNotFirstTime;
NSMutableArray *teamMembers;
NSMutableArray *projects;
NSMutableArray *tasks;


}
@property(readwrite, retain) NSMutableArray *teamMembers;
@property(readwrite, retain) NSMutableArray *projects;
@property(nonatomic, retain) NSMutableArray *tasks;
@property(nonatomic)BOOL isNotFirstTime;
@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;

@end

AppDelegate.m出于某种原因,当我创建文件 URL 时它仍然为空......我不知道为什么......

    #import "AppDelegate.h"
#import "Task.h"
#import "Project.h"
#import "TeamMember.h"
#import "newTeamMemberWindow.h"


@implementation AppDelegate

@synthesize window = _window;
@synthesize tasks;
@synthesize teamMembers;
@synthesize projects;
@synthesize isNotFirstTime;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
NSLog(@"No Context on App Load");
}

newTeamMemberWindow *newTeamMemberWindowObject = [[newTeamMemberWindow alloc]init];
newTeamMemberWindowObject.managedObjectContext = context;


return YES;

}

//删除了所有常规方法以在堆栈溢出时合并代码

#pragma mark - Core Data stack

/**
Returns the managed object context for the application.
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
*/
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}

/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created from the application's model.
*/
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}

这部分modelURL保持为Null.....

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TimeLines" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSLog(@"Created managedObjectModel with Url: %@", modelURL);
return __managedObjectModel;
}

/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TimeLines.sqlite"];

NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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.

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 the application's resources directory instead of a writeable directory.

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:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return __persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}





@end

最佳答案

如果我知道您有一个单独的核心数据项目实用程序,如果是,请查看我的这个问题。前段时间让我发疯!!!

How to include a bundle in main project xcode 4.1

关于objective-c - 如何将核心数据添加到现有的基于选项卡的 IOS 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9611147/

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