Objective-c CoreData
2015-12-16 00:00
435 查看
#import "ViewController.h"
#import "Person.h"//引入需要使用的实体
#import <CoreData/CoreData.h>
@interface ViewController ()
{
//管理对象上下文
NSManagedObjectContext *_context;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_context = [self createDBContext];
// [self addPersonWithName:@"测试员01" sex:@"男" age:@18];
// [self addPersonWithName:@"测试员02" sex:@"女" age:@16];
// [self addPersonWithName:@"测试员03" sex:@"男" age:@28];
NSLog(@"%@", [[self getPersonByName:@"测试员01"] valueForKey : @"sex"]);
// [self removePerson:@"测试员02"];
[self modifyPersonWithName:@"测试员03" sex:@"女" age:@100];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark 1.创建管理上下文
- (NSManagedObjectContext *)createDBContext
{
NSManagedObjectContext *context;
//打开模型文件,参数为nil则打开包中所有模型文件并合并成一个
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
//创建解析器NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//创建数据库保存路径(文件目录)
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", path);
path = [path stringByAppendingPathComponent:@"YC.db"];
//添加SQLite持久存储到解析器
NSError *error;
[storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if (error) {
NSLog(@"数据库打开失败!错误: %@", error.localizedDescription);
}
else//数据库创建成功(打开成功)
{
context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
context.persistentStoreCoordinator = storeCoordinator;
NSLog(@"数据库打开成功!");
}
return context;
}
#pragma mark 2.插入数据
- (void)addPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age
{
//创建实体对象
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];
person.name = name;
person.sex = sex;
person.age = age;
//保存上下文
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"添加过程中发送错误,错误: %@", error.localizedDescription);
}
else
{
NSLog(@"添加成功");
}
}
#pragma mark 3.查询数据
//NSPredicate(谓词)是一个Foundation的类,它指定数据被获取或者过滤的方式。它的语言就像SQL的where和正则表达式的交叉一样。提供了具有表现力的,自然语言界面来定义一个集合。(被搜索的逻辑条件)
//*zo* z, zo, zoo, zooo....
//zo+ zo, zoo, zooo, zoooo...
- (NSArray *)getPersonByName : (NSString *)name
{
//创建一个请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.predicate = [NSPredicate predicateWithFormat:@"%K=%@",@"name", name];
//上下文执行请求,得到查询结果
NSArray *array = [_context executeFetchRequest:request error:nil];
return array;
}
#pragma mark 4.删除数据
- (void)removePerson : (NSString *)name
{
Person *person = [[self getPersonByName:name] firstObject];
[_context deleteObject:person];
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"删除过程中发生错误:%@", error.localizedDescription);
}
else
{
NSLog(@"删除成功");
}
}
#pragma mark 修改数据
//必须首先提取出对应的实体对象,然后通过修改对象属性,最后保存
- (void)modifyPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age{
Person *person = [[self getPersonByName:name] firstObject];
person.sex = sex;
person.age = age;
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"修改过程中发生错误: %@", error.localizedDescription);
}
else
{
NSLog(@"修改成功");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "Person.h"//引入需要使用的实体
#import <CoreData/CoreData.h>
@interface ViewController ()
{
//管理对象上下文
NSManagedObjectContext *_context;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_context = [self createDBContext];
// [self addPersonWithName:@"测试员01" sex:@"男" age:@18];
// [self addPersonWithName:@"测试员02" sex:@"女" age:@16];
// [self addPersonWithName:@"测试员03" sex:@"男" age:@28];
NSLog(@"%@", [[self getPersonByName:@"测试员01"] valueForKey : @"sex"]);
// [self removePerson:@"测试员02"];
[self modifyPersonWithName:@"测试员03" sex:@"女" age:@100];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark 1.创建管理上下文
- (NSManagedObjectContext *)createDBContext
{
NSManagedObjectContext *context;
//打开模型文件,参数为nil则打开包中所有模型文件并合并成一个
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
//创建解析器NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//创建数据库保存路径(文件目录)
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", path);
path = [path stringByAppendingPathComponent:@"YC.db"];
//添加SQLite持久存储到解析器
NSError *error;
[storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if (error) {
NSLog(@"数据库打开失败!错误: %@", error.localizedDescription);
}
else//数据库创建成功(打开成功)
{
context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
context.persistentStoreCoordinator = storeCoordinator;
NSLog(@"数据库打开成功!");
}
return context;
}
#pragma mark 2.插入数据
- (void)addPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age
{
//创建实体对象
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];
person.name = name;
person.sex = sex;
person.age = age;
//保存上下文
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"添加过程中发送错误,错误: %@", error.localizedDescription);
}
else
{
NSLog(@"添加成功");
}
}
#pragma mark 3.查询数据
//NSPredicate(谓词)是一个Foundation的类,它指定数据被获取或者过滤的方式。它的语言就像SQL的where和正则表达式的交叉一样。提供了具有表现力的,自然语言界面来定义一个集合。(被搜索的逻辑条件)
//*zo* z, zo, zoo, zooo....
//zo+ zo, zoo, zooo, zoooo...
- (NSArray *)getPersonByName : (NSString *)name
{
//创建一个请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.predicate = [NSPredicate predicateWithFormat:@"%K=%@",@"name", name];
//上下文执行请求,得到查询结果
NSArray *array = [_context executeFetchRequest:request error:nil];
return array;
}
#pragma mark 4.删除数据
- (void)removePerson : (NSString *)name
{
Person *person = [[self getPersonByName:name] firstObject];
[_context deleteObject:person];
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"删除过程中发生错误:%@", error.localizedDescription);
}
else
{
NSLog(@"删除成功");
}
}
#pragma mark 修改数据
//必须首先提取出对应的实体对象,然后通过修改对象属性,最后保存
- (void)modifyPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age{
Person *person = [[self getPersonByName:name] firstObject];
person.sex = sex;
person.age = age;
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"修改过程中发生错误: %@", error.localizedDescription);
}
else
{
NSLog(@"修改成功");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- Objective-C中的内存管理
- CSS Object Model View整理
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread
- Objective-C 字符串一 "不可变字符串"
- Objective-C 字符串二 "可变字符串"
- (Foundation)NSObject 、 NSString 、 NSMutableString
- Objective-c---分类 、 扩展 、 ARC
- Objective-C语言介绍 、 Objc与C语言 、 面向对象编程 、 类和对象 、 属性和方法 、 属性和实例变量
- Objective-C--- 多态 、 协议
- Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式
- Objective-C----MRC内存管理 、 自动释放池 、 面向对象三大特性及封装 、 继承 、 组合与聚合
- Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式
- Objective-C语言介绍 、 Objc与C语言 、 面向对象编程 、 类和对象 、 属性和方法 、 属性和实例变量
- ArcEngine(ArcObjects) java开发环境配置
- Android学习第七天————将数据保存为JSON格式,通过JSONObject和JSONReader来解析JSON数据
- a different object with the same identifier value was already associated with the session
- Object类
- for,for-in和enumerateObjectsUsingBlock的比较
- python 安装pandas 出现InsecurePlatformWarning: A true SSLContext object is not available.
- Objective-c语言_复合