日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发之coreData

發(fā)布時(shí)間:2023/12/31 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发之coreData 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。