数据化持久话模式

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值