2013-10-17 102 views
0

我有一个问题:核心数据和NSManagedObject

我已经加入Model.xcdatamodeld

在这里面我有一个实体 - 设备有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; 

还是错误...

回答

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; 
} 
2

我不知道我的回答仍然有用与否的代码,但这个错误显示出来,因为您没有导入CoreData.framework也在你想要使用它的类中导入CoreData。

在你的班级中输入“CoreData/CoreData.h”(.h)

相关问题