核心数据和NSManagedObject

时间:2013-10-17 07:53:47

标签: ios sqlite core-data

我有下一个问题:

我添加了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数据库)获取设备信息,并将数据填充到表视图控制器”

这是否意味着Core数据是SQLite数据库?

ERROR:

未知类型名称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;

仍然是错误......

2 个答案:

答案 0 :(得分:2)

我不知道我的回答是否仍然有用但是此错误显示是因为您没有导入CoreData.framework也导入您要使用它的类中的CoreData。

import" CoreData / CoreData.h"在你的班级(.h)

答案 1 :(得分: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;
}