我有下一个问题:
我已经添加了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;
还是错误...