zoukankan      html  css  js  c++  java
  • 使用CoreData [1]

    使用CoreData [1]

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

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

    2. 添加一个实体类

    3. 创建出实体类

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

    以下是验证结果:

    - (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个对象.

    6. 遍历出所有的对象

    - (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. 删除一个对象

    - (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. 修改一个对象

    - (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. 根据谓词查找出实体的方法请自行百度脑补,这里不赘述了.

  • 相关阅读:
    阿里云ECS linux通过rinetd 端口转发来访问内网服务
    阿里云ECS linux通过iptables 配置SNAT代理网关,实现局域网上网
    适用于CentOS6.x系统的15项优化脚本
    ELK学习笔记
    MAC OSX环境下cordova+Ionic的安装配置
    Windows下 VM12虚拟机安装OS X 10.11 和VM TOOLS
    cordova 下载更新
    android adb常用命令
    ionic实现双击返回键退出功能
    ionic ngCordova插件安装
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3878444.html
Copyright © 2011-2022 走看看