ฉันกำลังพยายามเปรียบเทียบสตริงก่อนที่จะบันทึกลงใน Core Data
สตริงที่ได้รับการบันทึกลงใน Core Data จะมีรายการการออกกำลังกาย สตริงของแบบฝึกหัดจะต้องได้รับการบันทึกเพียงครั้งเดียวโดยไม่คำนึงถึงลำดับ
ตัวอย่าง:
let str1 = "Burpees Rowing Running"
// This is in Core Data
let str2 = "Running Rowing Burpees"
// This is an attempt to save to Core Data. It should *fail* because there is already an exercise set with these exercises - just not in the same order.
ความคืบหน้าของฉัน:
func checkEntityThenSave(exerciseGroup:String){
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "SavedExerciseSets")
let predicate = NSPredicate(format: "upperBody.sortedComponents == %@", exerciseGroup.components(separatedBy: " ").sorted())
request.predicate = predicate
request.fetchLimit = 1
do{
let count = try context.count(for: request)
print("Count - \(count)") // Always evaluates to 0
if(count > 0){
// Save to Core Data
}
else{
// Show Alert
}
}
catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
ในโค้ดของฉัน ฉันกำลังพยายามเปรียบเทียบผลลัพธ์ที่ดึงมา (สตริง) ใน Core Data กับสตริงใหม่ที่ฉันพยายามบันทึก
ปัญหาของฉันคือฉันได้รับ 0 เรื่อย ๆ ซึ่งทำให้ความพยายามในการบันทึกล้มเหลวทุกครั้ง
ฉันจะเปรียบเทียบสตริงที่ฉันพยายามบันทึกกับสตริงที่อยู่ใน Core Data ได้อย่างไร
upperBody
แอตทริบิวต์ลงในอาร์เรย์ของ Set จากนั้นตรวจสอบว่ามีชุดแรกอยู่ในอาร์เรย์นั้นหรือไม่ - person Joakim Danielson   schedule 01.04.2021SavedExerciseSets
มีอยู่ใน CoreData ของคุณจริง ๆ"Running Rowing Burpees"
หรือไม่ ฉันหมายถึงตัวพิมพ์ใหญ่ทั้งหมดที่จุดเริ่มต้นของแต่ละคำเหรอ? - person Larme   schedule 04.04.2021upperBody
มีแอตทริบิวต์sortedComponents
เช่น อาร์เรย์ที่เรียงลำดับของสตริงหรือไม่ หากเป็นเช่นนั้น โค้ดของคุณควรใช้งานได้ ไม่เช่นนั้นจะไม่ทำงาน เนื่องจาก SQL ไม่สามารถแยกย่อยและเรียงลำดับแอตทริบิวต์สตริงก่อนที่จะดึงข้อมูลได้ - person Reinhard Männer   schedule 04.04.2021