#import "ViewController.h" // 第一步:引入头文件AppDelegate
#import "AppDelegate.h"
#import "Person.h"
#import "Car.h" @interface ViewController () // 第二步:声明属性[管理对象上下文](SQLite中是声明一个存储路径的属性)
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; @end @implementation ViewController // 懒加载
- (NSManagedObjectContext *)managedObjectContext { // 因为在AppDelegate中已经实现过了,所以在这里是从AppDelegate中去获取
if (!_managedObjectContext) {
// 获取appDelegate对象
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
_managedObjectContext = appDelegate.managedObjectContext;
}
return _managedObjectContext;
} - (void)viewDidLoad {
[super viewDidLoad]; // 添加Person
[self addPerson]; // 删除Personf
// [self deletePerson]; // 更改person信息
// [self updatePerson]; // 查询所有人
[self searchAllPerson]; // 更改car的信息
[self updateCar]; } #pragma mark - 添加person
- (void)addPerson { // 添加的步骤
// 1.创建person实体对象,然后告诉context,让他做好准备,将这个对象添加到数据库中
// 实例对象有两种方式,(初始化实体对象需要借助NSEntityDescription)
// 第一种:(简单,用的多)
Person *per = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; // 第二种:(不常用)
// 创建一个entity的实体
// NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
// // 创建person对象
// Person *per = [[Person alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.managedObjectContext]; // 2.给对象属性赋值
per.personName = @"卫庄";
per.personGender = @"男";
per.personAge = @; /**
* 将车的对象通过实体描述类创建出来
*/
Car *audiCar = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:self.managedObjectContext]; audiCar.price = @;
audiCar.color = @"白色";
audiCar.brand = @"奥迪A7"; Car *benchiCar = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:self.managedObjectContext]; benchiCar.price = @;
benchiCar.color = @"红色";
benchiCar.brand = @"奔驰SUV"; // 将两辆车放到集合中存储并进行赋值
NSSet *carSet = [NSSet setWithObjects:audiCar, benchiCar, nil];
per.personCar = carSet; // 3.将person对象存入数据库
BOOL result = [_managedObjectContext save:nil]; // 4.判断是否插入成功
if (result) {
NSLog(@"添加数据成功");
} else {
NSLog(@"添加数据失败");
} } #pragma mark - 删除person
- (void)deletePerson { // 1.实体化请求类【查询】
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; // 2.找到删除的条件(即根据什么去删除)(NSPredicate)
request.predicate = [NSPredicate predicateWithFormat:@"personName = '卫庄'"]; // 3.由context根据删除条件的请求去具体进行删除操作
NSArray *resultArray = [self.managedObjectContext executeFetchRequest:request error:nil]; // 4.遍历搜索出来的结果
for (Person *per in resultArray) { // 删除查询的相关的人的信息
[self.managedObjectContext deleteObject:per]; } // 5.进行删除结果的判断
BOOL result = [self.managedObjectContext save:nil]; // 6.判断结果
if (result) {
NSLog(@"删除成功");
} else {
NSLog(@"删除失败");
} } #pragma mark - 更改person信息
- (void)updatePerson { // 1.实例化查询请求类
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; // 2.设置查询条件
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"personName = '卫庄'"]; // 3.由管理对象上下文“context”,按照查询条件进行相关的操作
NSArray *resultArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; // 4.遍历结果更新数据
for (Person *per in resultArray) { // 更改相关的信息
per.personName = @"高月";
per.personGender = @"女";
per.personAge = @; NSLog(@"name = %@, gender = %@, age = %@", per.personName, per.personGender, per.personAge);
} // 5.进行更新结果的判断
BOOL result = [self.managedObjectContext save:nil]; // 6.判断是否更新成功
if (result) {
NSLog(@"更新成功");
} else {
NSLog(@"更新失败");
} } #pragma mark - 查询所有人
- (void)searchAllPerson { // 1.实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; // 2.设置查询条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personName LIKE '*月'"];
request.predicate = predicate; // 3.根据管理对象上下文“context”执行相关的操作
NSArray *resultArray = [self.managedObjectContext executeFetchRequest:request error:nil]; // 4.遍历查询结果
for (Person *per in resultArray) { NSLog(@"name = %@, gender = %@, age = %@", per.personName, per.personGender, per.personAge);
} // 5.进行查询结果的判断
BOOL result = [self.managedObjectContext save:nil]; // 6.判断是否查询成功
if (result) {
NSLog(@"查询成功");
} else {
NSLog(@"查询失败");
}
} #pragma mark - 更新车的相关信息
- (void)updateCar { // 1.实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Car"]; // 2.设置查询条件
request.predicate = [NSPredicate predicateWithFormat:@"brand CONTANS '奥迪A7'"]; // 3.管理对象上下文按照查询条件去查询
NSArray *resultArray = [self.managedObjectContext executeFetchRequest:request error:nil]; // 4.遍历查询结果
for (Car *car in resultArray) { NSLog(@"price = %@, brand = %@, color = %@", car.price, car.brand, car.color);
car.brand = @"宝马X5"; } // 5.保存结果进行结果判断
if ([self.managedObjectContext save:nil]) {
NSLog(@"更新成功");
} else {
NSLog(@"更新失败");
} } @end