昨晚突然有個朋友問起coredata的使用,后來便自己寫了個demo看看,發(fā)現(xiàn)跟Xcode7使用有些不一樣,今天有時間就記下來跟大家分享下。
創(chuàng)建工程時勾選use core data
打開工程時會發(fā)現(xiàn)比多一個.xcdatamodeld文件,名字和工程名一致,demo工程名為CoreDataDemo。
選中該文件 點(diǎn)擊add Entity 可創(chuàng)建實(shí)體,需大寫開頭,可以理解為model,同時可添加屬性,選擇屬性類型
然后就是Xcode8之后的差異了。
第一個圖是之前新建一個file,NSManagedObjectSubclass。
第二個截圖是現(xiàn)在Xcode的界面,取消了NSManagedObjectSubclass。
選中創(chuàng)建的Entity,點(diǎn)擊導(dǎo)航欄的Editor-->Create NSManagedObjectSubclass,然后按照提示操作下去,會生成四個文件
細(xì)心的小伙伴可能還會發(fā)現(xiàn)AppDelegate跟平時我們創(chuàng)建的有些不同,多了一個coredata操作對象及方法。
//
// AppDelegate.h
// CoreDataDemo
//
// Created by simon on 2017/5/3.
// Copyright ? 2017年 simon. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
@end
那我們現(xiàn)在來寫個增刪改查的操作。
#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "Dog+CoreDataClass.h"
@interface ViewController ()
{
AppDelegate *_app;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[self createBtns];
}
- (void)createBtns{
NSArray *titles = @[@"增",@"刪",@"改",@"查"];
for (int i = 0; i < titles.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(30 + i * 60, 100, 40, 40);
[btn setTitle:titles[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
[self.view addSubview:btn];
}
}
- (void)btnAction:(UIButton *)btn{
switch (btn.tag) {
case 0:
[self addCoreData];
break;
case 1:
[self deleteCoreData];
break;
case 2:
[self updateCoreData];
break;
case 3:
[self selectCoreData];
break;
default:
break;
}
}
增加一條數(shù)據(jù):
- (void)addCoreData{
Dog *dog = [NSEntityDescription insertNewObjectForEntityForName:@"Dog" inManagedObjectContext:_app.persistentContainer.viewContext];
static NSInteger index = 0;
dog.name = [NSString stringWithFormat:@"tom%ld",index++];
dog.sex = @"公";
dog.age = [NSString stringWithFormat:@"%uyears",arc4random() % 15];
[_app saveContext];
}
查詢所有數(shù)據(jù):
- (void)selectCoreData{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:_app.persistentContainer.viewContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSArray *array = [_app.persistentContainer.viewContext executeFetchRequest :request error:nil];
for (Dog *dog in array) {
NSLog(@"dog name:%@",dog.name);
}
}
如需條件查詢可使用 NSPredicate 添加查詢條件。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@",@"tom0"];
request.predicate = predicate;
刪除一條數(shù)據(jù):
- (void)deleteCoreData{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:_app.persistentContainer.viewContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = entity;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@",@"tom0"];
request.predicate = predicate;
NSArray *array = [_app.persistentContainer.viewContext executeFetchRequest:request error:nil];
if (array.count) {
for (Dog *dog in array) {
[_app.persistentContainer.viewContext deleteObject:dog];
}
NSLog(@"DELETE SUCCESS!!");
} else
NSLog(@"NOT FOUND DATA");
}
更新某條數(shù)據(jù):
- (void)updateCoreData{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:_app.persistentContainer.viewContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = entity;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@",@"tom0"];
request.predicate = predicate;
NSArray *array = [_app.persistentContainer.viewContext executeFetchRequest:request error:nil];
if (array.count) {
for (Dog *dog in array) {
dog.name = @"jerry8";
}
[_app saveContext];
NSLog(@"UPDATA SUCCESS!!");
} else
NSLog(@"NOT FOUND DATA");
}
順便問下,用chrome訪問簡書,用markdown模式寫這些字,chrome崩潰十幾次了,輸入文字就崩,不知道是不是添加高亮代碼塊的鍋,有沒有大神遇到過!(后來這些字都是在文本編輯器寫好貼上去的)
參考:http://www.lxweimin.com/p/337872d95727