数据化持久话模式

1,首先创建一个实体类(student)

2,进入程序

在viewcontroller.m中

#import "ViewController.h"

#import "AppDelegate.h"

#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //获取程序中的appdelegate对象,以便使用coredata的相关的属性和方法;

    AppDelegate*appdelegate=[UIApplication sharedApplication].delegate;

    //存数据

    //初始化实体,注意@“ ”里直接放实体类名字

    Student*s=[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:appdelegate.managedObjectContext];

    s.name=@"李吧";

    s.age=[NSNumber numberWithInteger:12];

    [appdelegate saveContext];//可写可不写 ,目的是保存数据

   // NSLog(@"%@",NSHomeDirectory());

//在初始化一个实体对象

    Student*s1=[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:appdelegate.managedObjectContext];

    s1.name=@"王二";

    s1.age=[NSNumber numberWithInteger:14];

    [appdelegate saveContext];

    //创建查询实体,若没有设置查询条件,则默认查询存储所有的东西;

    NSFetchRequest*request=[[NSFetchRequest alloc]init];//创建空语句

   NSEntityDescription*en=[NSEntityDescription entityForName:@"Student" inManagedObjectContext:appdelegate.managedObjectContext];//创建要查询的实体

    

    [request setEntity:en];//将要查询的实体放入到查询语句中;

NSArray*arr=[appdelegate.managedObjectContext executeFetchRequest:request error:nil];//执行查询语句,并且返回查询结果

    for(Student *s3 in arr)

    {

        NSLog(@"%@ %@",s3.name,s3.age);

}

输出结果为

因为上面没有添加查询条件,所以输出是所有存进去的数据

将存进去的数据进行改动;

把便利换一下就可以实现

 for(Student *s3 in arr)

    {

        NSLog(@"%@ %@",s3.name,s3.age);

        s1.name=@"张三";

        s1.age=[NSNumber numberWithInteger:20];

        s.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*array=[appdelegate.managedObjectContext executeFetchRequest:f1 error:nil];//执行查询语句。并且返回查询结果;

    for (Student*ss in array)

    {

       // NSLog(@"%@ %@",ss.name,ss.age);

}

输出的结果为

下面的代码实现数据的删除操作;

    

    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/wpw19920808/p/5008113.html