美文网首页
swift3.0 coreData的基本使用,简单实现增删改查

swift3.0 coreData的基本使用,简单实现增删改查

作者: 嗯哼丶傻大个是你 | 来源:发表于2017-05-04 16:07 被阅读0次
    1.png
    2.png
    3.png

    三步走完,上代码,简单实现增删改查
    1.增

    let app = UIApplication.shared.delegate as! AppDelegate
        let contexts = app.persistentContainer.viewContext
        
        let entityName = "Person"
        let onePerson = NSEntityDescription.insertNewObject(forEntityName: entityName, into: contexts) as! Person
        
        onePerson.name = "nnn"
        onePerson.uid = "111"
        onePerson.url = "2333"
        app.saveContext()
    

    2.删

    let app = UIApplication.shared.delegate as! AppDelegate
        let contexts = app.persistentContainer.viewContext
        let entityName = "Person"
        
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest()
        fetchRequest.fetchLimit = 10
        fetchRequest.fetchOffset = 0
        
        let entity = NSEntityDescription.entity(forEntityName: entityName, in: contexts)
        fetchRequest.entity = entity
        
        let predicate = NSPredicate.init(format: "name = 'nnn'", "")
        fetchRequest.predicate = predicate
        
        
        do {
          
          let fetchedObjects = try contexts.fetch(fetchRequest) as! [Person]
          for one: Person in fetchedObjects {
            contexts.delete(one)
            app.saveContext()
          }
          
        } catch  {
          let nserror = error as NSError
          fatalError("查询错误: \(nserror), \(nserror.userInfo)")
          
    

    3.改

    let app = UIApplication.shared.delegate as! AppDelegate
        let contexts = app.persistentContainer.viewContext
        let entityName = "Person"
        
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest()
        fetchRequest.fetchLimit = 10
        fetchRequest.fetchOffset = 0
        
        let entity = NSEntityDescription.entity(forEntityName: entityName, in: contexts)
        fetchRequest.entity = entity
        
        let predicate = NSPredicate.init(format: "name = 'nnn'", "")
        fetchRequest.predicate = predicate
        
        
        do {
          
          let fetchedObjects = try contexts.fetch(fetchRequest) as! [Person]
          print(fetchedObjects)
          for one in fetchedObjects {
            
            one.uid = "123"
            app.saveContext()
          }
          
        } catch  {
          let nserror = error as NSError
          fatalError("查询错误: \(nserror), \(nserror.userInfo)")
          
        }
    

    4.查

    let app = UIApplication.shared.delegate as! AppDelegate
        let contexts = app.persistentContainer.viewContext
        let entityName = "Person"
        
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest()
        fetchRequest.fetchLimit = 10
        fetchRequest.fetchOffset = 0
        
        let entity = NSEntityDescription.entity(forEntityName: entityName, in: contexts)
        fetchRequest.entity = entity
        
        let predicate = NSPredicate.init(format: "name = 'nnn'", "")
        fetchRequest.predicate = predicate
        
        do {
          
          let fetchedObjects = try contexts.fetch(fetchRequest) as! [Person]
          for one: Person in fetchedObjects {
            print("==========\(String(describing: one.name))")
            print("==========\(String(describing: one.uid))")
            print("==========\(String(describing: one.url))")
          }
          
        } catch  {
          let nserror = error as NSError
          fatalError("查询错误: \(nserror), \(nserror.userInfo)")
          
        }
    

    demo: https://github.com/BJGX/UseOfCoreData

    相关文章

      网友评论

          本文标题:swift3.0 coreData的基本使用,简单实现增删改查

          本文链接:https://www.haomeiwen.com/subject/ayfdtxtx.html