IOS开发之coreData
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
注意在使用coreData的時(shí)候,注意創(chuàng)建工程的時(shí)候要勾選use core Data這一個(gè)選項(xiàng),然后操作如下圖:添加實(shí)體和實(shí)體類,里面和操作數(shù)據(jù)庫的步驟是一樣的。
然后點(diǎn)擊新建,點(diǎn)擊如藍(lán)色部分的新建類:
//coreData是蘋果提供的數(shù)據(jù)本地化的一種,實(shí)質(zhì)還是通過數(shù)據(jù)庫存儲(chǔ)數(shù)據(jù)
//但是操作數(shù)據(jù)的時(shí)候不需要sql語句
//核心文件和類
//1.模型文件:(后綴是.xcdatamodeld),相當(dāng)于數(shù)據(jù)庫文件
//2.實(shí)體:相當(dāng)于表
//3.實(shí)體類:表存儲(chǔ)的對(duì)象的類
//4.上下文:關(guān)聯(lián)模型文件和實(shí)際生成的數(shù)據(jù)庫文件;
//coreData的使用步驟
//1.創(chuàng)建一個(gè)模型文件;(創(chuàng)建工程點(diǎn)use coredata就創(chuàng)建了)
//2.創(chuàng)建實(shí)體,相當(dāng)于創(chuàng)建表
//3.創(chuàng)建實(shí)體類
//4.生成上下文:(操作上下文就相當(dāng)于操作數(shù)據(jù)庫)
#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
@interface ViewController ()
//上下文
@property (nonatomic,strong) NSManagedObjectContext *context;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //插入數(shù)據(jù)
? //? [self insertData];
?? ?
? ? //數(shù)據(jù)查詢
? ? //[self selectData];
?? ?
? ? //刪除數(shù)據(jù)
? ? //[self deleteData];
?? ?
? ? //更新數(shù)據(jù)
? ? [self updataData];
? ? NSLog(@"%@",NSHomeDirectory());
?? ?
?? ?
}
#pragma mark - 更新數(shù)據(jù)
- (void)updataData{
? ? //1.查詢到需要更新的數(shù)據(jù)
? ? NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]
? ? ? ? ? ? ? ? ? ? initWithEntityName:@"Student"];
?? ?
? ? //創(chuàng)建查詢對(duì)象
? ? NSPredicate *pre = [NSPredicate predicateWithFormat:@"score<70"];
? ? //設(shè)置查詢條件
? ? fetchrequest.predicate = pre;
? ? //查詢
? ? NSArray *array = [self.context executeFetchRequest:fetchrequest error:nil];
?? ?
?? ?
? ? //2.更新屬性
? ? for (Student *stu in array) {
?? ? ? ?
? ? ? ? stu.score = @60;
? ? ? ?
? ? }
? ?
? ? //3.保存數(shù)據(jù)庫
? ? [self.context save:nil];
?? ?
}
#pragma mark - 刪除數(shù)據(jù)
- (void)deleteData{
?? ?
? ? //1.查詢要?jiǎng)h除的數(shù)據(jù)
? ? NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithEntityName:@"Student"];
? ? //創(chuàng)建查詢對(duì)象
? ? NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = '小明'"];
? ? //設(shè)置查詢條件
? ? fetchrequest.predicate = pre;
? ? //查詢
? ? NSArray *array = [self.context executeFetchRequest:fetchrequest error:nil];
? ?
? ? //2.刪除數(shù)據(jù)
? ? for (Student *stu in array) {
?? ? ? ?
? ? ? ? [self.context deleteObject:stu];
? ? }
?? ?
?? ?
? ? //3.保存數(shù)據(jù)庫
? ? [self.context save:nil];
?? ?
?? ?
}
#pragma mark - 數(shù)據(jù)查詢
- (void)selectData{
?? ?
? ? //1.創(chuàng)建一個(gè)查詢對(duì)象
? ? //參數(shù):實(shí)體名:相當(dāng)于表名
? ? NSFetchRequest *fetchrequest = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
? ? //2.查詢數(shù)據(jù)
? ? //返回值:查詢結(jié)果數(shù)據(jù)
? ? NSArray *array = [self.context executeFetchRequest:fetchrequest error:nil];
?? ?
? ? for (Student *stu in array) {
? ? ? ? [stu show];
? ? }
?? ?
? ? //==========按條件查詢==================
? ? //1.創(chuàng)建條件對(duì)象
? ? //條件:成績(jī)大于80
? ? NSPredicate *pred = [NSPredicate predicateWithFormat:@"score > 80"];
?? ?
? ? NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"score > %@",@90];
?? ?
? ? NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"%K > %@",@"age",@15];
?? ?
? ? //2.設(shè)置查詢對(duì)象的條件
? ? fetchrequest.predicate = pred3;
?? ?
? ? //3.查詢數(shù)據(jù)
? ? NSArray *array2 = [self.context executeFetchRequest:fetchrequest error:nil];
? ? for (Student *stu in array2) {
? ? ? ? [stu show];
? ? }
?? ?
? ? //===============對(duì)查詢結(jié)果進(jìn)行排序======================
? ? //1.創(chuàng)建排序?qū)ο?/span>
? ? //參數(shù)1:按哪個(gè)屬性進(jìn)行排序
? ? //參數(shù)2:是否升序
? ? NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
? ? NSSortDescriptor *desc1 = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
?? ?
? ? //2.設(shè)置查詢的排序
? ? fetchrequest.sortDescriptors = @[desc,desc1];
?? ?
? ? //3.查詢數(shù)據(jù)
?? NSArray *array3 = ? [self.context executeFetchRequest:fetchrequest error:nil];
? ? for (Student *stu in array3) {
? ? ? ? [stu show];
? ? }
?? ?
?? ?
?? ?
}
#pragma mark - 數(shù)據(jù)的插入
-(void)insertData{
?? ?
? ? //=============插入單條數(shù)據(jù)======================
? ? //1.通過實(shí)體創(chuàng)建一個(gè)學(xué)生對(duì)象
?? ?
? ? //參數(shù)1:實(shí)體名(表名)
? ? //參數(shù)2:上下文(數(shù)據(jù)庫)
? ? Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
?? ?
? ? //2.設(shè)置學(xué)生對(duì)象的屬性
? ? student.name = @"小明";
? ? student.age = @20;
? ? student.score = @90;
?? ?
? ? //保存數(shù)據(jù)庫
? ? [self.context save:nil];
?? ?
?? ?
?? ?
? ? //========插入多條數(shù)據(jù)===========
?? ?
? ? for (int i =0 ; i<20; i++) {
? ? ? ? //1.創(chuàng)建對(duì)象
? ? ? ? Student *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
? ? ? ? //2.設(shè)置屬性
? ? ? ? stu.name = [NSString stringWithFormat:@"name%d",i];
? ? ? ? stu.age = [NSNumber numberWithInt:arc4random() % 10 +10];
? ? ? ? stu.score = [NSNumber numberWithFloat:arc4random() % 100];
?? ? ? ?
?? ? ? ?
? ? }
? ? //3.保存數(shù)據(jù)庫
? ? [self.context save:nil];
?? ?
}
#pragma? mark - 懶加載
- (NSManagedObjectContext *)context{
?? ?
? ? if (_context == nil) {
? ? ? ?
? ? ? ? //1.拿到當(dāng)前應(yīng)用程序的代理
? ? ? ? //拿到當(dāng)前應(yīng)用程序?qū)ο?/span>
? ? ? ? //生成上下文
? ? ? ? AppDelegate *delegate =? [UIApplication sharedApplication].delegate;
? ? ? ? _context = delegate.managedObjectContext;
?? ? ? ?
?? ? ? ?
?? ? ? ?
?? ? ? ?
? ? }
? ? return _context;
}
@end
轉(zhuǎn)載于:https://my.oschina.net/luhoney/blog/670508
總結(jié)
以上是生活随笔為你收集整理的IOS开发之coreData的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隐秘之鱼在哪里
- 下一篇: Oozie JMS通知消息实现--根据作