1.Core Data 是数据持久化存储的最佳方式
2.数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型
3.好处:能够合理管理内存,避免使用sql的麻烦,高效
4.注意CoreData的创建方式
使用步骤 : 创建数据库- - -获取程序中的appdelegate对象 - - 然后就可以进行增查改删
增加步骤 :1.初始化实体--2.增加数据 - -3.保存coredata数据
查询步骤 : 1.创建一个空语句 - -2.创建要查询的实体--3.创建要查询的语句—--4.对返回的结果进行遍历
Student.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Student : NSManagedObject
#import <CoreData/CoreData.h>
@interface Student : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * age;
@end
======================================================
Student.m
#import "Student.h"
@implementation Student
@dynamic name;
@dynamic age;
@dynamic name;
@dynamic age;
@end
======================================================
ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取程序中的appdelegate对象,以便使用coredata的相关属性及方法(coredata的属性和方法在appdelegate.h中存着)
AppDelegate * _appdelegate = [UIApplication sharedApplication].delegate;
//coredata存储数据========增加数据=============
//初始化实体,注意@“”里面是存放实体类名
Student * s = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];
s.name = @"我是一把神经刀";
s.age = [NSNumber numberWithInteger:22];
[_appdelegate saveContext];//用来保存coredata数据
NSLog(@"%@",NSHomeDirectory());
// NSLog(@"%@",s);
Student * s10 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];
s10.name = @"小莫";
s10.age = [NSNumber numberWithInteger:30];
[_appdelegate saveContext];
// NSLog(@"%@",s10);
#import "AppDelegate.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取程序中的appdelegate对象,以便使用coredata的相关属性及方法(coredata的属性和方法在appdelegate.h中存着)
AppDelegate * _appdelegate = [UIApplication sharedApplication].delegate;
//coredata存储数据========增加数据=============
//初始化实体,注意@“”里面是存放实体类名
Student * s = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];
s.name = @"我是一把神经刀";
s.age = [NSNumber numberWithInteger:22];
[_appdelegate saveContext];//用来保存coredata数据
NSLog(@"%@",NSHomeDirectory());
// NSLog(@"%@",s);
Student * s10 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];
s10.name = @"小莫";
s10.age = [NSNumber numberWithInteger:30];
[_appdelegate saveContext];
// NSLog(@"%@",s10);
==============查询数据===================
//查询:只有1、2、3句时,代表查询所有,5、6结合,设置查询条件
/*1*/ NSFetchRequest * request = [[NSFetchRequest alloc] init];//创建一个空语句
/*2*/NSEntityDescription * en = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];//创建要查询的实体
/*3*/[request setEntity:en];//创建要查询的语句
/*4*/NSPredicate * p = [NSPredicate predicateWithFormat:@"age > %d",20];//这是查询条件
/*5*/[request setPredicate:p];//将查询条件放入查询语句
[_appdelegate saveContext];
NSArray * arr1 = [_appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句,并且返回查询结果
for (Student *s in arr1) {//-------对返回的结果进行遍历
NSLog(@"%@,%@",s.name,s.age);
}
//==============修改数据=============
NSArray * arr = [_appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句
for (Student * s1 in arr) {
s10.name = @"张三";//替换数据
s10.age = [NSNumber numberWithInteger:32];
NSLog(@"%@",s1.name);
}
//修改数据每次只能修改一个,想要修改多个,需要重新创建修改
NSArray * arr2 = [_appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句
for (Student * s2 in arr2) {
s.name = @"张小花";//替换数据
s.age = [NSNumber numberWithInteger:100];
NSArray * arr1 = [_appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句,并且返回查询结果
for (Student *s in arr1) {//-------对返回的结果进行遍历
NSLog(@"%@,%@",s.name,s.age);
}
//==============修改数据=============
NSArray * arr = [_appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句
for (Student * s1 in arr) {
s10.name = @"张三";//替换数据
s10.age = [NSNumber numberWithInteger:32];
NSLog(@"%@",s1.name);
}
//修改数据每次只能修改一个,想要修改多个,需要重新创建修改
NSArray * arr2 = [_appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句
for (Student * s2 in arr2) {
s.name = @"张小花";//替换数据
s.age = [NSNumber numberWithInteger:100];
NSLog(@"%@",s2.name);
}
// ============删除=====================
NSFetchRequest *f1 = [[NSFetchRequest alloc] init];
NSEntityDescription *en1 = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];
[f1 setEntity:en1];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name = %@",@"王五"];
[f1 setPredicate:p1];
NSArray *rArr = [_appdelegate.managedObjectContext executeFetchRequest:f1 error:nil];
for (Student *s in rArr) {
[_appdelegate.managedObjectContext deleteObject:s];
}
NSFetchRequest *f2 = [[NSFetchRequest alloc] init];
[f2 setEntity:en1];
NSArray *r2 = [_appdelegate.managedObjectContext executeFetchRequest:f2 error:nil];
NSLog(@"%@",r2);
NSFetchRequest *f1 = [[NSFetchRequest alloc] init];
NSEntityDescription *en1 = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:_appdelegate.managedObjectContext];
[f1 setEntity:en1];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name = %@",@"王五"];
[f1 setPredicate:p1];
NSArray *rArr = [_appdelegate.managedObjectContext executeFetchRequest:f1 error:nil];
for (Student *s in rArr) {
[_appdelegate.managedObjectContext deleteObject:s];
}
NSFetchRequest *f2 = [[NSFetchRequest alloc] init];
[f2 setEntity:en1];
NSArray *r2 = [_appdelegate.managedObjectContext executeFetchRequest:f2 error:nil];
NSLog(@"%@",r2);
来源:https://www.cnblogs.com/yibadao/p/5022675.html