我不希望 FetchRequest 返回[QuestionCD]
数组。FetchRequest 不能返回QuestionCD
给我吗?
每个测试都有一个唯一的标题。一次测试总共有 50 道题。这里的标题决定了问题的类别。
使用此功能,我根据所选测试的标题保存该测试的问题。
我的核心数据模型错了吗?我想按标题对每个测试进行分类。我希望根据搜索的标题将问题返回给我。例如:“标题:八月测试 2”。请查看我的 json 构造和 coreData 模型。
保存分类功能:
func saveSelectedCategory(title: String) {
let allCategory = QuestionCD(context: persistentContainer.viewContext)
allCategory.title = title
do {
try persistentContainer.viewContext.save()
} catch {
print("Failed to save selected category: \(error.localizedDescription)")
}
}
保存类别后,我将问题以数组的形式传递到同一模型中的问题参数中并保存。
保存问题功能:
[QuestionList]
是我的定制模型。我正在使用这个模型将我从 json 得到的问题保存到 CoreData。
func saveSelectedQuestion(questions: [QuestionList]) {
let question = QuestionCD(context: persistentContainer.viewContext)
question.questions = questions
do {
try persistentContainer.viewContext.save()
} catch {
print("Failed to save selected category: \(error.localizedDescription)")
}
}
获得选定的问题:
这里我根据选中的标题在QuestionCD中查找选中标题的类别。您可以查看 json 构造。例如标题:“Ağustos 测试 2”
我不希望返回结果是"[QuestionCD]"
. 我希望返回的结果是QuestionCD
. 这可能吗 ?
您可以从 Core Data Entites 图像中检查 QuestionCD 模型。 如果 QuestionCD 不是数组,我可以在单个 ForEach 中运行它来获取问题。
func getSelectedQuestion(questionID: String) -> [QuestionCD] {
let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
let search = NSPredicate(format: "title CONTAINS %@", questionID)
print("search: \(search)")
fetchRequest.predicate = search
do {
return try persistentContainer.viewContext.fetch(fetchRequest)
} catch {
return []
}
}
核心数据实体:
JSON:
{
"allQuiz": [
{
"title":"Ağustos Test 1",
"questions": [
{
"id": "1",
"question":"Şekle göre aşağıdakiler hangisi doğrudur",
"isQuestionImage": true,
"isSectionImage": false,
"imageURL":"https://firebasestorage.googleapis.com/v0/b/ehliyet-sinavim-01.appspot.com/o/Agustos%20Test%201%2F1.png?alt=media&token=2881447c-9081-4b13-a7ad-3ad097886b04",
"sections": {
"A":"2 numaralı aracın öncelikle geçmesi",
"B":"1 numaralı aracın hızını arttırarak kavşağa girmesi",
"C":"2 numaralı aracın 3 numaralı aracın geçmesini beklemesi",
"D":"3 numaralı aracın 2 numaralı aracı ikaz ederek durdurması"
},
"selected":"",
"correct": "A"
},
{
"id": "2",
"question":"Akaryakıt istasyonundan yola çıkmak isteyen şekildeki 2 numaralı araç sürücüsü ne yapmalıdır ?",
"isQuestionImage": true,
"isSectionImage": false,
"imageURL":"https://firebasestorage.googleapis.com/v0/b/ehliyet-sinavim-01.appspot.com/o/Agustos%20Test%201%2F2.jpg?alt=media&token=94b833ff-3462-445a-9054-94b3cdaaa668",
"sections": {
"A":"Selektör yaparak 1 numarlı aracı durdurmalıdır.",
"B":"Korna çalıp 1 numralı aracı yavaşlatmalıdır.",
"C":"1 numaralı aracın geçmesini beklemelidir.",
"D":"Geçiş hakkını kendi kullanmalıdır."
},
"selected":"",
"correct": "C"
},
...........
]
},
{
"title":"Ağustos Test 2",
"questions": [
{
"id": "1",
"question":"Şekle göre aşağıdakiler hangisi doğrudur",
"isQuestionImage": true,
"isSectionImage": false,
"imageURL":"https://firebasestorage.googleapis.com/v0/b/ehliyet-sinavim-01.appspot.com/o/Agustos%20Test%201%2F1.png?alt=media&token=*****-a7ad-3ad097886b04",
"sections": {
"A":"2 numaralı aracın öncelikle geçmesi",
"B":"1 numaralı aracın hızını arttırarak kavşağa girmesi",
"C":"2 numaralı aracın 3 numaralı aracın geçmesini beklemesi",
"D":"3 numaralı aracın 2 numaralı aracı ikaz ederek durdurması"
},
"selected":"",
"correct": "A"
},
{
"id": "2",
"question":"Akaryakıt istasyonundan yola çıkmak isteyen şekildeki 2 numaralı araç sürücüsü ne yapmalıdır ?",
"isQuestionImage": true,
"isSectionImage": false,
"imageURL":"https://firebasestorage.googleapis.com/v0/b/ehliyet-sinavim-01.appspot.com/o/Agustos%20Test%201%2F2.jpg?alt=media&token=*****-9054-94b3cdaaa668",
"sections": {
"A":"Selektör yaparak 1 numarlı aracı durdurmalıdır.",
"B":"Korna çalıp 1 numralı aracı yavaşlatmalıdır.",
"C":"1 numaralı aracın geçmesini beklemelidir.",
"D":"Geçiş hakkını kendi kullanmalıdır."
},
"selected":"",
"correct": "C"
},
...........
]
}
]
}
模型:
class QuestionContainer: NSObject, Codable{
var questions: Question
init(questions: Question) {
self.questions = questions
}
}
class Question: NSObject, Codable {
var title: String
var questions: [QuestionList]
init(title: String, questions: [QuestionList]) {
self.title = title
self.questions = questions
}
}
public class QuestionList: NSObject, Codable {
var id: String
var question: String
var isQuestionImage, isSectionImage: Bool
var imageURL: String
var imageData: Data?
var sections: [QuestionSections.RawValue : String]
var selected: String
var correct: String
init(id: String, question: String, isQuestionImage: Bool, isSectionImage: Bool, imageURL: String, sections: [QuestionSections.RawValue : String], selected: String, correct: String) {
self.id = id
self.question = question
self.isQuestionImage = isQuestionImage
self.isSectionImage = isSectionImage
self.imageURL = imageURL
self.sections = sections
self.selected = selected
self.correct = correct
}
}
核心数据管理器:
class CoreDataManager: ObservableObject {
let persistentContainer: NSPersistentContainer
init() {
persistentContainer = NSPersistentContainer(name: "EhliyetSinavim")
persistentContainer.loadPersistentStores { description, error in
if let error = error {
fatalError("Core Data Stre failed: \(error.localizedDescription)")
}
}
}
func saveSelectedCategory(title: String) {
let allCategory = QuestionCD(context: persistentContainer.viewContext)
allCategory.title = title
do {
try persistentContainer.viewContext.save()
} catch {
print("Failed to save selected category: \(error.localizedDescription)")
}
}
func getSelectedCategory() -> [QuestionCD] {
let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
let sort = NSSortDescriptor(key: "title", ascending: true)
fetchRequest.sortDescriptors = [sort]
do {
return try persistentContainer.viewContext.fetch(fetchRequest)
} catch {
return []
}
}
func searchInCategory(text: String) -> [QuestionCD] {
let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
let search = NSPredicate(format: "ANY title == %@", text)
print("search: \(search)")
fetchRequest.predicate = search
print("request predicate: \(String(describing: fetchRequest.predicate))")
do {
return try persistentContainer.viewContext.fetch(fetchRequest)
} catch {
print("ver bulunamadı \n")
return []
}
}
//MARK: ForQuestionCategory
func saveSelectedQuestion(title: String, questions: [QuestionList]) {
let question = QuestionCD(context: persistentContainer.viewContext)
question.title = title
question.questions = questions
do {
try persistentContainer.viewContext.save()
} catch {
print("Failed to save selected category: \(error.localizedDescription)")
}
}
func getSelectedQuestion(questionID: String) -> [QuestionCD] {
let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
let search = NSPredicate(format: "title CONTAINS %@", questionID)
print("search: \(search)")
fetchRequest.predicate = search
do {
return try persistentContainer.viewContext.fetch(fetchRequest)
} catch {
return []
}
}
}