2023-11-13|閱讀時間 ‧ 約 29 分鐘

[swift]Core Data新增、更新、查詢、刪除

用來操作 Core Data 的常數

private let coreDataContext = (NSApplication.shared.delegate
as! AppDelegate).persistentContainer.viewContext


取的資料數量:

func getCount() -> Int {
do{
let result = try coreDataContext.fetch(Member.fetchRequest())
for data in result as! [Member] {
if let name = data.value(forKey: "name") as? String {
print(name)
}
}
return result.count
}catch let error as NSError{
print("getCount->\(error)")
}
return 0
}


刪除:

    
func getCount() -> Int {
do{
let result = try coreDataContext.fetch(Member.fetchRequest())
for data in result as! [Member] {
if let name = data.value(forKey: "name") as? String {
print(name)
}

}
return result.count
}catch let error as NSError{
print("getCount->\(error)")
}
return 0
}

func deleteCoreData(name:String="") -> Bool {
let request : NSFetchRequest = Member.fetchRequest()
if name=="" {
return false
}

request.predicate = NSPredicate(format: "name = %@",name)

do{
let result = try coreDataContext.fetch(request)
for data in result {
coreDataContext.delete(data)
}
try coreDataContext.save()
return true
}catch let error as NSError{
print("read->\(error)")
return false
}
}


新增:

func writeCoreData(name:String) -> Bool {
let memberEntity = Member(context: coreDataContext)
memberEntity.id = NSDecimalNumber.init(value: getCount()+1)
memberEntity.name = name
memberEntity.createdate = Int(NSDate().timeIntervalSince1970).description

do{
try coreDataContext.save()
return true
}catch let error as NSError{
print("read->\(error)")
return false
}

}


更新:

func updateCoreData(oldName:String="",newName:String="") -> Bool {
let request : NSFetchRequest = Member.fetchRequest()
if oldName=="" || newName==""{
return false
}
// request.predicate = nil
request.predicate = NSPredicate(format: "name = %@",oldName)

do{
let result = try coreDataContext.fetch(request)
for data in result {
data.name = newName
data.createdate = Int(NSDate().timeIntervalSince1970).description
}
try coreDataContext.save()
return true
}catch let error as NSError{
print("read->\(error)")
return false
}
}


查詢:

func readCoreData(name:String) -> Array<Any> {
let request : NSFetchRequest = Member.fetchRequest()
//CONTAINS:查詢特定內容的資料
request.predicate = NSPredicate(format: "name CONTAINS %@ ",name)

//BEGINSWITH:查詢特定開頭的資料
request.predicate = NSPredicate(format: "name BEGINSWITH %@ ",name)

//ENDSWITH:查詢特定結尾的資料
request.predicate = NSPredicate(format: "name ENDSWITH %@ ",name)

//like:查詢特定結尾的資料(以星號的位置決定)
request.predicate = NSPredicate(format: "name like %@ ","*"+name)

//like:查詢特定開頭的資料
request.predicate = NSPredicate(format: "name like %@ ",name+"*")

//=:查詢內容要跟資料完全一樣
request.predicate = NSPredicate(format: "name = %@ ",name)

var arr = Array<Any>()

do{
let result = try coreDataContext.fetch(request)
for data in result {
let dic = NSMutableDictionary()

if let name = data.value(forKey: "name") as? String {
dic.setObject(name, forKey: "name" as NSCopying)
dic.setObject(data.value(forKey: "createdate") as! String, forKey: "createdate" as NSCopying)
dic.setObject(data.value(forKey: "id") as! NSDecimalNumber, forKey: "id" as NSCopying)
// print(name)
}
arr.append(dic)
}
}catch let error as NSError{
print("read->\(error)")
}
return arr
}
分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.