iOS大典之Core Data

来源:互联网 发布:在淘宝买衣服好吗 编辑:程序博客网 时间:2024/06/05 06:59

Core Data


用来保存应用数据


这里写图片描述



创建Single View工程,
要勾上 Use Core Data 的复选框,

创建数据模型 :
点击选中CoreData.xcdatamodeld, 然后commond + N, 出来数据模型编辑界面,点击面板左下方的加号图标(Add Entity), 创建个Entity实体, 双击名字可以修改

添加属性 :
点加号, 输入名字, 并更改type


补充下基本信息:

创建数据模型: 使用 Core Data . 创建实体(Entity), 在数据模型编辑器中, 然后用代码创建托管对象(managed object).

实体和托管对象的关系, 约等于 类和类的实例 的关系

特性 Attributes 关系 Relationships 提取属性 Fetched Properties

实体的属性:
1 . 特性; 用来保存数据, 比如:年龄,姓名,三围,
2 . 关系; 是一对一, 还是一对多. 如: 偶像实体和粉丝实体, 偶像和粉丝的关系就用到这个属性了, 偶像到粉丝的关系是一对多, 粉丝到偶像是一对一
3 . 提取属性; 关系的备胎. 用提取属性可以创建个提取时被评估的查询, 从而确定对象属于哪个这个关系


代码如下 :


属性声明 被管理对象上下文(临时数据库)

#import "AppDelegate.h"
@property (nonatomic, strong) NSManagedObjectContext *managerContext;

添加数据

// 创建实体描述    NSEntityDescription *studentDE = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];    for (int i = 0; i < 10; i ++ ) {        // 根据实体描述创建实体        Student *student = [[Student alloc] initWithEntity:studentDE insertIntoManagedObjectContext:self.managerContext];        // 给实体赋值        student.name = [NSString stringWithFormat:@"君君%d号", i];        student.age = [NSNumber numberWithInt:(i + 5)];        student.sex = [NSString stringWithFormat:@"男"];    }    NSError *error = nil;    // 数据同步    [self.managerContext save:&error];    if (!error) {        NSLog(@"存储成功");    }

查询数据

 // 从实体中查询    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];    // 实体描述    NSEntityDescription *studentDE = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];    //  设置查询实体    request.entity = studentDE;    // 设置排序, yes升序, no降序    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];    request.sortDescriptors = @[sort];    // 设置查询 根据指定条件查询    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"君君8号"];    request.predicate = predicate;    // 模糊查询,    NSPredicate *predicatee = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"8"];    request.predicate = predicatee;    // 头尾查询    NSPredicate *predicateee = [NSPredicate predicateWithFormat:@"name LIKE %@", @"君君*"];    request.predicate = predicateee;    // 数组接受查询的数据    NSArray *array = [self.managerContext executeFetchRequest:request error:nil];    // 便利数组 取出数据打印    for (Student *student in array) {        NSLog(@"姓名 : %@, 年纪 : %@, 性别 : %@", student.name, student.age, student.sex);    }

修改数据

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];    NSEntityDescription *studentDE = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];    request.entity = studentDE;    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"君君8号"];    request.predicate = predicate;    NSArray *array = [self.managerContext executeFetchRequest:request error:nil];    for (Student *student in array) {        student.age = [NSNumber numberWithInt:44];        student.sex = @"nv";        student.name = @"君君1000号";        NSLog(@"姓名 : %@, 年纪 : %@, 性别 : %@", student.name, student.age, student.sex);    }    NSError *error = nil;    // 数据同步    [self.managerContext save:&error];    if (!error) {        NSLog(@"修改成功");    }

删除数据

 NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];    NSEntityDescription *studentDE = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerContext];    request.entity = studentDE;    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"君君9号"];    request.predicate = predicate;    NSArray *array = [self.managerContext executeFetchRequest:request error:nil];    // 便利数组 取出数据打印    for (Student *student in array) {        [self.managerContext deleteObject:student];    }    NSError *error = nil;    // 数据同步    [self.managerContext save:&error];    if (!error) {        NSLog(@"删除成功");    }

over


0 0
原创粉丝点击