CoreData 欢迎来到CoreData 使用。

1  创建  


记得要点上这个

2. 就是模版的建立了。




3. 用于添加的方法。

两种方法。1!!!

<span style="font-size:24px;">    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    Person *person = [[Person alloc]initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];</span>
<span style="font-size:24px;">
</span>
<span style="font-size:24px;">
</span>
<span style="font-size:24px;">
</span>
2!!

Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];

我喜欢第二种 就是一句话写成了。


3.就是查找了

NSFetchRequest *fetch = [[NSFetchRequest alloc]initWithEntityName:@"Person"];
    //谓词晒选
    fetch.predicate = [NSPredicate predicateWithFormat:@"name = '随便'"];
    
    NSArray *array = [self.managedObjectContext executeFetchRequest:fetch error:nil];



这个谓词就是查找时的条件。

比如 我想找person这个里面  条件是名字是随便的中的


查找第二种 就是打上 fetch 就出来语法块了  

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
     Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@ ", @"张三"];
    [fetchRequest setPredicate:predicate];
     Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"error");
    };
    NSLog(@"%@",fetchedObjects);
    
    Person *suibian = fetchedObjects[0];
    [self.managedObjectContext deleteObject:suibian];

每次写完 记得保存 

 ************[self saveContext]*******************


完事了。好用又好玩/

陈佳鸿原创出品




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值