首页 > 代码库 > [iOS] Core Data 代码速查表
[iOS] Core Data 代码速查表
文中代码均来源于:http://www.appcoda.com/introduction-to-core-data/
希望学习 Core Data 的同学不要错过:)以下是我个人记录的一些常用代码片段。
有一个 Entity:Device,有三个属性:company、name、version。
1.获取 context 的方法:
- (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context; }
2.增加一条数据:
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]); }
3.删除一条数据:
NSManagedObjectContext *context = [self managedObjectContext]; // Delete object from database [context deleteObject:[self.devices objectAtIndex:indexPath.row]]; NSError *error = nil; if (![context save:&error]) { NSLog(@"Can‘t Delete! %@ %@", error, [error localizedDescription]); return; }
4.修改一条数据:
NSManagedObjectContext *context = [self managedObjectContext]; // Update existing device [self.device setValue:self.nameTextField.text forKey:@"name"]; [self.device setValue:self.versionTextField.text forKey:@"version"]; [self.device 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]); }
5. 查询一堆数据:
// Fetch the devices from persistent data store NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"]; self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[iOS] Core Data 代码速查表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。