本人菜鸟,有什么不对的还望大神指正.
#import "ViewController.h"
#import "AppDelegate.h"
#import "Person+CoreDataProperties.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
1. 自动生成模型类步骤:
1.1 选中文件.xcdatamodeld
1.2 Editor -> Create NSManagedObject SubClass..
1.3 按照引导步骤去完成。
2. 当项目需要迭代,模型中需要增加新的属性,如何操作:
2.1 选中.xcdatamodeld文件,修改模型增加属性;
2.2 EDitor -> Add Model Version, 创建新的model版本(用数字标记版本)
2.3 选中.xcdatamodeld文件,将右侧分栏打开,找到Model Version 修改当前要生效的版本
2.4 重新生成新的模型,会覆盖(参考1.2)
3. coreData单例模式
AppDelegate * coreData = [UIApplication sharedApplication].delegate;
coreData.managedObjectContext
*/
}
// 最终调用只能通过点语法 self.coreData
-(AppDelegate *)coreData
{
if(_coreData != nil){
return _coreData;
}
_coreData = [UIApplication sharedApplication].delegate;
return _coreData;
}
#pragma mark - CoreData 操作(增删改查)
- (IBAction)insertPerson:(id)sender {
// 创建一个实体描述类
// 第一个参数:实体名称 要注意大小写
NSEntityDescription * personDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.coreData.managedObjectContext];
// 创建最终的模型对象
Person * person = (Person *)[[NSManagedObject alloc] initWithEntity:personDesc insertIntoManagedObjectContext:self.coreData.managedObjectContext];
// 对属性做出改变
person.name = self.nameTextFeild.text;
person.age = [NSNumber numberWithInteger:[self.ageTextFeild.text integerValue]];
person.height = [NSNumber numberWithFloat:[self.heightTextField.text floatValue]];
// 执行插入操作
[self.coreData.managedObjectContext insertObject:(NSManagedObject *)person];
NSError * error = nil;
// 保存上下文
BOOL isInsert = [self.coreData.managedObjectContext save:&error];
if(!isInsert){
// 如果发生错误,抛出异常
[NSException raise:@"插入数据失败" format:@"%@", error.debugDescription];
}
}
- (IBAction)deletePerson:(id)sender {
// 创建一个查询请求
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
// 创建谓词,查询的条件
// eg: 查询出所有以Zh开头的数据 @"name like %@", @"Zh*"
// eg2:查询年龄小于30的所有数据
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"age < %ld", 30];
request.predicate = predicate;
//
request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.coreData.managedObjectContext];
NSError * error = nil;
NSArray * result = [self.coreData.managedObjectContext executeFetchRequest:request error:&error];
// 得到最终结果,删除结果中的第一条数据
if(result.count > 0){
[self.coreData.managedObjectContext deleteObject:[result firstObject]];
NSError * saveError = nil;
BOOL isUpdate = [self.coreData.managedObjectContext save:&saveError];
if(!isUpdate){
[NSException raise:@"删除数据异常" format:@"%@", saveError.debugDescription];
}
}
}
- (IBAction)updatePerson:(id)sender {
// 创建一个查询请求
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
// 创建谓词,查询的条件
// eg: 查询出所有以Zh开头的数据 @"name like %@", @"Zh*"
// eg2:查询年龄小于30的所有数据
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"age < %ld", 30];
request.predicate = predicate;
//
request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.coreData.managedObjectContext];
NSError * error = nil;
NSArray * result = [self.coreData.managedObjectContext executeFetchRequest:request error:&error];
if(result.count > 0){
Person * per = result[0];
// oc 中的特点:“字面量” @24 @176.5 @"" @[] @{}
per.age = @24;
NSError * saveError = nil;
BOOL isUpdate = [self.coreData.managedObjectContext save:&saveError];
if(!isUpdate){
[NSException raise:@"更新数据异常" format:@"%@", saveError.debugDescription];
}
}
}
- (IBAction)selectPerson:(id)sender {
// 创建一个查询请求
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
// 创建谓词,查询的条件
// eg: 查询出所有以Zh开头的数据 @"name like %@", @"Zh*"
// eg2:查询年龄小于30的所有数据
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"age < %ld", 30];
request.predicate = predicate;
//
request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.coreData.managedObjectContext];
NSError * error = nil;
NSArray * result = [self.coreData.managedObjectContext executeFetchRequest:request error:&error];
for(Person * per in result){
NSLog(@"%@, %@, %@", per.name, per.age, per.height);
}
}
@end
权当做个笔记,以后在补充吧