CoreData的基本操作

CoreData 的基本操作

import UIKit
import CoreData

class ViewController: UIViewController {

    @IBOutlet weak var tfAge: UITextField!
    @IBOutlet weak var tfName: UITextField!
    
    // 在相应界面生成一个 全局变量
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    
    // 数据的删除
    @IBAction func del(_ sender: Any) {
    // 获取内容 
        let context = appDelegate.persistentContainer.viewContext
        
        // Person这个数据库
        let request: NSFetchRequest = Person.fetchRequest()
        
        
        // 查询条件
        request.predicate = NSPredicate(format: "name = %@", tfName.text!)
        
        // 排序。根据名字。年龄排序  true 为 升序  false为 逆序
        let sort1 = NSSortDescriptor(key: "age", ascending: true)
        let sort2 = NSSortDescriptor(key: "name", ascending: false)
        request.sortDescriptors = [sort1,sort2]
        
        if let persons = try? context.fetch(request) {
            for p in persons {
                // 删除符合 条件的 行
                context.delete(p)
                appDelegate.saveContext()
            }
        }
    }
    
    
    
    @IBAction func update(_ sender: Any) {
        let context = appDelegate.persistentContainer.viewContext
        
        let request: NSFetchRequest = Person.fetchRequest()
        request.predicate = NSPredicate(format: "name = %@", tfName.text!)
        let sort1 = NSSortDescriptor(key: "age", ascending: true)
        let sort2 = NSSortDescriptor(key: "name", ascending: false)
        request.sortDescriptors = [sort1,sort2]
        
        if let persons = try? context.fetch(request) {
            for p in persons {
                p.age = Int32(tfAge.text!)!
                appDelegate.saveContext()
            }
        }
    }
    
    
    
    @IBAction func add(_ sender: Any) {
        let context = appDelegate.persistentContainer.viewContext
        
        // 创建一个对象
        let p = Person(context: context)
        p.name = tfName.text!
        p.age = Int32(tfAge.text!)!
        
        appDelegate.saveContext()
    }
    
    
    @IBAction func search(_ sender: Any) {
        let context = appDelegate.persistentContainer.viewContext
        
        
        let request: NSFetchRequest = Person.fetchRequest()
        
        // 查询条件
        
        request.predicate = NSPredicate(format: "name = %@", tfName.text!)
        let sort1 = NSSortDescriptor(key: "age", ascending: true)
        let sort2 = NSSortDescriptor(key: "name", ascending: false)
        request.sortDescriptors = [sort1,sort2]
        
        
        if let persons = try? context.fetch(request) {
            for p in persons {
                print("name = \(p.name!) age= \(p.age)")
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


你可能感兴趣的:(CoreData的基本操作)