0

我有下一个问题:

我已经添加了Model.xcdatamodeld

在其中我有 1 个实体 - 具有 3 个属性的设备:名称、版本、公司;

在我的DetailController我添加了这个:

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
    context = [delegate managedObjectContext];
    }
    return context;
}

在按钮上单击我想保存数据:

- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];

//Create a new managed object
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
/*


[newDevice setValue:self.nameTextField.text forKey:@"name"];
[newDevice setValue:self.versionTextField.text forKey:@"version"];
[newDevice setValue:self.companyTextField.text forKey:@"company"];

NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];
 */
}

它会引发错误NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];

我做错了什么?也许如果我手动添加 DataModel 我应该把它连接到某个地方?

第二个问题是理论上的:

我阅读了有关在 XCODE 中开发的教程,我目前阅读了有关 CoreData 的内容,它说:

引用:“从持久存储(即 SQLite 数据库)中获取设备信息并将数据填充到表视图控制器中”

这是否意味着核心数据是 SQLite 数据库?

错误:

未知类型名称NSManagedObject;你的意思是NSManagedObjectModel

编辑了我的代码:

添加CoreData.framework

将此添加到AppDelegate.h

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

将此添加到AppDelegate.m

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

还是错误...

4

2 回答 2

2

我不知道我的回答是否仍然有用,但出现此错误是因为您没有导入 CoreData.framework 并且在您想要使用它的类中导入 CoreData。

在您的班级(.h)中导入“CoreData/CoreData.h”

于 2015-11-04T04:09:41.970 回答
0

这是您需要添加到 AppDelegate.m 的代码

- (void)saveContext
{
  NSError *error = nil;
  NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
  if (managedObjectContext != nil)
  {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
    {
        //TODO Proper error handling
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 
  }
}

#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;
  }
  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyAppTesting" withExtension:@"momd"];
  __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: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:@"MyAppTesting.sqlite"];

  NSError *error = nil;
  __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
  {
    //TODO Error handling 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  }    

  return __persistentStoreCoordinator;
}
于 2013-10-17T13:58:24.757 回答