我正在努力拥有一个非常简单的核心数据。我有一个像这样的ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIResponder <UIApplicationDelegate>;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (IBAction)boutonSave:(id)sender;
@end
和ViewController.m一样:
#import "ViewController.h"
@implementation ViewController
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (IBAction)boutonSave:(id)sender {
ViewController *viewController = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [viewController managedObjectContext];
NSManagedObject *newJournee; //THIS LINE IN ERROR
newJournee = [NSEntityDescription insertNewObjectForEntityForName:@"JourneeDeTravail" inManagedObjectContext:context];
[newJournee setValue:_displayStart.text forKey:@"debut"];
[newJournee setValue:_displayEnd.text forKey:@"fin"];
}
但是在创建newJournee时,我有一个红色警告“未知类型名称'NSManagedObject',你的意思是NSManagedObjectModel?” ???这是我的第一个核心数据,所以我当然错过了一些对你来说显而易见的东西......但不是我!帮助!
答案 0 :(得分:19)
添加CoreData.framework并导入如下
#import <CoreData/CoreData.h>
希望它有所帮助〜
答案 1 :(得分:5)
听起来你错过了#import
答案 2 :(得分:0)
别担心。一切似乎都很好......
只做一件事。转到你的ViewController.m文件并添加这一行。
#import "ViewController.h"
#import <CoreData/CoreData.h>
@implementation ViewController
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (IBAction)boutonSave:(id)sender {
ViewController *viewController = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [viewController managedObjectContext];
NSManagedObject *newJournee; //Error removed
newJournee = [NSEntityDescription insertNewObjectForEntityForName:@"JourneeDeTravail" inManagedObjectContext:context];
[newJournee setValue:_displayStart.text forKey:@"debut"];
[newJournee setValue:_displayEnd.text forKey:@"fin"];
希望这会有所帮助。