二次封装CoreData
(1)创建一个Data Model文件。命名为MyModel.xcdatamodeld
(2)创建Users表,加入如图的字段
(3)创建NSManagedObject subclass表实体文件
(4)在Users.m文件里,覆写description方法
CoreDataDBHelper.h
#import <Foundation/Foundation.h> #import <CoreData/CoreData.h> // 本地文件存储的路径 #define PATH [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/sqlite.db"] #define MODEL_NAME @"MyModel" @interface CoreDataDBHelper : NSObject { // 1.数据模型对象 NSManagedObjectModel *_managedObjectModel; // 2.创建本地持久文件对象 NSPersistentStoreCoordinator *_persistentStoreCoordinator; // 3.管理数据对象 NSManagedObjectContext *_managedObjectContext; } // 设计成单例模式 + (CoreDataDBHelper *)shareCoreDataDBHelper; // 加入数据的方法 - (BOOL)insertDataWithModelName:(NSString *)modelName setAttributWithDic:(NSDictionary *)params; // 查看 /* modelName :实体对象类的名字 predicateString :谓词条件 identifers :排序字段集合 ascending :是否升序 */ - (NSArray *)selectDataWithModelName:(NSString *)modelName predicateString:(NSString *)predicateString sort:(NSArray *)identifers ascending:(BOOL)ascending; // 改动 - (BOOL)updateDataWithModelName:(NSString *)modelName predicateString:(NSString *)predicateString setAttributWithDic:(NSDictionary *)params; // 删除 - (BOOL)deleteDataWithModelName:(NSString *)modelName predicateString:(NSString *)predicateString; @end
CoreDataDBHelper.m
#import "CoreDataDBHelper.h" @implementation CoreDataDBHelper - (instancetype)init { self = [super init]; if (self) { // 1.数据模型对象 NSURL *modelUrl = [[NSBundle mainBundle] URLForResource:MODEL_NAME withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl]; // 2.创建本地持久文件对象 _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_managedObjectModel]; // 设置本地数据的保存位置 NSURL *fileUrl = [NSURL fileURLWithPath:PATH]; [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileUrl options:nil error:nil]; // 3.管理数据对象 _managedObjectContext = [[NSManagedObjectContext alloc] init]; [_managedObjectContext setPersistentStoreCoordinator:_persistentStoreCoordinator]; } return self; } // 设计成单例模式 + (CoreDataDBHelper *)shareCoreDataDBHelper { static CoreDataDBHelper *coreDataDBHelper = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ coreDataDBHelper = [[CoreDataDBHelper alloc] init]; }); return coreDataDBHelper; } // 加入数据的方法 /* params : @{ @"name":@"张三", @"age":@20 } */ - (BOOL)insertDataWithModelName:(NSString *)modelName setAttributWithDic:(NSDictionary *)params { NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:modelName inManagedObjectContext:_managedObjectContext]; // 遍历參数字典 for (NSString *key in params) { SEL selector = [self selWithKeyName:key]; if ([entity respondsToSelector:selector]) { [entity performSelector:selector withObject:params[key]]; } } [_managedObjectContext insertObject:entity]; // 保存到本地 return [_managedObjectContext save:nil]; } // 查看 /* modelName :实体对象类的名字 predicateString :谓词条件 identifers :排序字段集合 ascending :是否升序 */ - (NSArray *)selectDataWithModelName:(NSString *)modelName predicateString:(NSString *)predicateString sort:(NSArray *)identifers ascending:(BOOL)ascending { // 1.创建实体对象 NSEntityDescription *entity = [NSEntityDescription entityForName:modelName inManagedObjectContext:_managedObjectContext]; // 2.创建一个查询对象 NSFetchRequest *request = [[NSFetchRequest alloc] init]; // 告诉查询对象你要查询的数据类型 [request setEntity:entity]; // 加入查询条件 if (predicateString != nil || [predicateString isEqualToString:@""]) { NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString]; [request setPredicate:predicate]; } // 3.设置排序 NSMutableArray *sortDescriptors = [NSMutableArray array]; for (NSString *identifer in identifers) { // 创建排序对象 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:identifer ascending:ascending]; // 把排序对象加入到数组中 [sortDescriptors addObject:sortDescriptor]; } // 把排序对象设置到查询对象里面 [request setSortDescriptors:sortDescriptors]; // 3.開始查询 return [_managedObjectContext executeFetchRequest:request error:nil]; } // 改动 - (BOOL)updateDataWithModelName:(NSString *)modelName predicateString:(NSString *)predicateString setAttributWithDic:(NSDictionary *)params { // 获取全部须要改动实体对象 NSArray *entitys = [self selectDataWithModelName:modelName predicateString:predicateString sort:nil ascending:NO]; // 遍历全部的实体对象 for (NSEntityDescription *entity in entitys) { // 改动对象的属性 for (NSString *key in params) { SEL selector = [self selWithKeyName:key]; if ([entity respondsToSelector:selector]) { [entity performSelector:selector withObject:params[key]]; } } } return [_managedObjectContext save:nil]; } // 删除 - (BOOL)deleteDataWithModelName:(NSString *)modelName predicateString:(NSString *)predicateString { // 获取全部须要改动实体对象 NSArray *entitys = [self selectDataWithModelName:modelName predicateString:predicateString sort:nil ascending:NO]; // 遍历全部的实体对象 for (NSEntityDescription *entity in entitys) { // 删除对象 [_managedObjectContext deleteObject:entity]; } return [_managedObjectContext save:nil]; } // 通过一个字符串反回一个set方法 - (SEL)selWithKeyName:(NSString *)keyName { NSString *first = [[keyName substringToIndex:1] uppercaseString]; NSString *end = [keyName substringFromIndex:1]; NSString *selString = [NSString stringWithFormat:@"set%@%@:",first,end]; return NSSelectorFromString(selString); } @end
– (NSString *)description
{
return [NSString
stringWithFormat:@”id:%@,name:%@,age;%@”,self.userId,self.userName,self.age];
}