码迷,mamicode.com
首页 > 其他好文 > 详细

使用CoreData [1]

时间:2014-07-30 17:18:54      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   os   io   

使用CoreData [1]

bubuko.com,布布扣

本篇教程能教你从无开始接触CoreData,包括新建工程,创建出实体,增删改查样样都有,属于使用CoreData最初级教程.

 

1. 创建带有CoreData的工程项目

bubuko.com,布布扣

 

2. 添加一个实体类

bubuko.com,布布扣

 

3. 创建出实体类

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

 

4. 创建对象,保存对象,执行代码

bubuko.com,布布扣

以下是验证结果:

bubuko.com,布布扣

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"%@", NSHomeDirectory());
    
    // 实体描述信息
    NSEntityDescription *description =         [NSEntityDescription entityForName:@"Student"
                    inManagedObjectContext:[self managedObjectContext]];
    
    // 初始化对象
    Student *student = [[Student alloc] initWithEntity:description
                        insertIntoManagedObjectContext:[self managedObjectContext]];
    student.name     = @"YouXianMing";
    student.age      = [NSNumber numberWithInt:26];
    
    // 保存对象
    [self saveContext];

    return YES;
}

这样就实现了存储对象.

 

5. 重复上面的步骤存储5个对象.

bubuko.com,布布扣

 

6. 遍历出所有的对象

bubuko.com,布布扣

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 设定要查询的实体
    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
    
    // 取出查询结果
    NSArray *students = [[self managedObjectContext] executeFetchRequest:fetch error:nil];
    
    // 遍历
    [students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        Student *student = obj;
        NSLog(@"%@ %@", student.age, student.name);
    }];

    return YES;
}

 

7. 删除一个对象

bubuko.com,布布扣

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 设定要查询的实体
    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
    
    // 取出查询结果
    NSArray *students = [[self managedObjectContext] executeFetchRequest:fetch error:nil];
    
    // 遍历
    [students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        Student *student = obj;

        // 找到匹配的数据,删除之
        if ([student.name isEqualToString:@"QiuLiang"])
        {
            [[self managedObjectContext] deleteObject:student];
        }
    }];
    
    // 存储
    [self saveContext];
    
    // 遍历
    [students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        Student *student = obj;
        
        NSLog(@"%@ %@", student.age, student.name);
    }];

    return YES;
}

 

8. 修改一个对象

bubuko.com,布布扣

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 设定要查询的实体
    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
    
    // 取出查询结果
    NSArray *students = [[self managedObjectContext] executeFetchRequest:fetch error:nil];
    
    // 遍历
    [students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        Student *student = obj;

        // 找到匹配的数据,修改之
        if ([student.name isEqualToString:@"YouXianMing"])
        {
            student.age = [NSNumber numberWithInt:100];
        }
    }];
    
    // 存储
    [self saveContext];
    
    // 遍历
    [students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        Student *student = obj;
        
        NSLog(@"%@ %@", student.age, student.name);
    }];

    return YES;
}

 

9. 根据谓词查找出实体的方法请自行百度脑补,这里不赘述了.

 

使用CoreData [1],布布扣,bubuko.com

使用CoreData [1]

标签:des   style   blog   http   color   使用   os   io   

原文地址:http://www.cnblogs.com/YouXianMing/p/3878444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!