Swift Coredata

2018-03-16  本文已影响10人  br_MorningStar
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entityDes = NSEntityDescription.entity(forEntityName: "Clothes", in: context)
let model = Clothes.init(entity: entityDes!, insertInto: context)
model.name = " aaa"
model.price = Int64(arc4random() % 1000 + 1)
model.ccc = "NN"
dataSrouce.append(model)
try? context.save()
context.delete(model)
try? context.save()
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Clothes")
let pre = NSPredicate(format: "price < %d", 300)
        
fetch.predicate = pre
 let deleArray = try? context.fetch(fetch)
for model in deleArray! {
    context.delete(model as! Clothes)
     dataSrouce.remove(at: dataSrouce.index(of: model as! Clothes)!)
  }
 try? context.save()
 tableView.reloadData()
let model = dataSrouce[indexPath.row]
model.name = "CC"
model.price = Int64(arc4random() % 10 + 1)
tableView.reloadRows(at: [indexPath], with: .top)
try? context.save()
  //  查询
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Clothes")
        
 //  排序描述对象
let sortDes = NSSortDescriptor(key: "price", ascending: true)        
fetch.sortDescriptors = [sortDes]        
//  执行查询
//        let result : NSPersistentStoreRequest
  do {
     let result = try context.fetch(fetch)
    for model in result {
       dataSrouce.append((model as! Clothes))
        }
   }catch{
        print(error)
    }

let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Clothes")
        let pre = NSPredicate(format: "price > %d", 4)
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
let pre = NSPredicate(format: "name like %@","*aaa*")
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
let pre = NSPredicate(format: "name like %@ and price = 510","*aaa*")
或者let pre = NSPredicate(format: "name like %@ and price = %i","*aaa*",510)
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
let pre = NSPredicate(format: "name like %@ or price = %i","*CC*",554)
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
let pre = NSPredicate(format: "name in %@",[" aaa","CC"])
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
let pre = NSPredicate(format: "price between {300,700}")
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
q 结尾的
let pre = NSPredicate(format: "name endswith[d] 'q'")
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
w 开头的
   let pre = NSPredicate(format: "name beginswith[d] 'w'")
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
let pre = NSPredicate(format: "name contains[d] 'A'")
        fetch.predicate = pre
        let array = try? context.fetch(fetch)
        for model in array! {
            print((model as! Clothes).price)
        }
如果有写错地方请帮我留言! 我是技术渣渣
上一篇 下一篇

猜你喜欢

热点阅读