002 Core Data系列

简单的demo演示Core Data是如何工作的

Core Data存储数据
func saveName(name: String) {
        
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        let managedContext = appDelegate.managedObjectContext
        let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedContext)
        
        
        let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
        person.setValue(name, forKey: "name")
        
        do {
            try managedContext.save()
        }catch {
            print("Error")
        }
        
        people.append(person)
        
    }

Core Data读取数据

    override func viewWillAppear(animated: Bool) {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        
        let fetchRequest = NSFetchRequest(entityName: "Person")
        
        var fetchRequests = [NSManagedObject]()
        
        do {
            fetchRequests = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
        }catch {
            
        }
        
        self.people = fetchRequests
        
        
    }``` 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,207评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,457评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,408评论 19 139
  • lemon: 你好! 今天是2016年8月23日,因为看到微博中有人会给未来的自己写一封信,这一点触动了我,我想给...
    温暖了时光阅读 2,118评论 0 1
  • 以下软件测试描述以楼主目前测试的B/S架构为基础----银行理财系统 1、软件测试:顾名思义测试系统满足客户需求并...
    笑笑爱笑smile阅读 3,647评论 0 9