这是这里问题的延续
我有这样的课程:
class Waterz {
var waterzEntitle: String
var waterzArtitle: String
var waterzIsFavorited: Bool?
var waterzDate: [Date]?
var waterzFrequency: [String]?
var waterzSubMenus: [Waterz]? = []
var waterzContent: String?
var waterzLocation: String?
var waterzAudio: String?
init(entitle: String, artitle: String, isFavorited: Bool?, date: [Date]?, frequency: [EKRecurrenceDayOfWeek]?, subMenus: [Waterz]?, content: String?, location: String?, audio: String?) {
waterzEntitle = entitle
waterzArtitle = artitle
waterzIsFavorited = isFavorited
waterzDate = date
waterzFrequency = frequency
waterzSubMenus = subMenus
waterzContent = content
waterzLocation = location
waterzAudio = audio
}
}
另一个类是我的数据库:
class WaterzBank {
// main categories
var mainCategories: Waterz
// individual categories
var duaKumayl: Waterz
var duaFaraj: Waterz
var duas: Waterz
var salatGhufayla: Waterz
var salatLaylImportance: Waterz
var salatLaylMethod: Waterz
var salatLayl: Waterz
var prayers: Waterz
var amal: Waterz
// more variables here
var favorites: Waterz
init() {
// MARK: - Duas
duaKumayl = Waterz(entitle: "Dua Kumayl", artitle: "دعاء كميل", isFavorited: true, date: nil, frequency: nil, subMenus: nil, content: "kumayl", location: nil, audio: nil)
duaFaraj = Waterz(entitle: "Dua Al-Faraj", artitle: "دعاء الفرج", isFavorited: false, date: ["10-21", "2-15"], frequency: ["friday"], subMenus: nil, content: "faraj", location: nil, audio: nil)
duas = Waterz(entitle: "Duas", artitle: "الأدعية", isFavorited: nil, date: nil, frequency: nil, subMenus: [duaKumayl, duaMakarem, duaNudba, duaSabah, duaSamat, duaAtharat, duaAhd, duaTawassul, duaFaraj], content: nil, location: nil, audio: nil)
// MARK: - Amal
salatGhufayla = Waterz(entitle: "Salat Al-Ghufayla", artitle: "صلاة الغفيلة", isFavorited: false, date: nil, frequency: nil, subMenus: nil, content: "salatGhufayla", location: nil, audio: nil)
salatLaylMethod = Waterz(entitle: "Method", artitle: "كيفيّة صلاة الليل", isFavorited: false, date: nil, frequency: nil, subMenus: nil, content: "salatLaylMethod", location: nil, audio: nil)
salatLaylImportance = Waterz(entitle: "Importance of Salat Al-Layl", artitle: "فضل صلاة الليل", isFavorited: false, date: nil, frequency: nil, subMenus: nil, content: "salatLaylImportance", location: nil, audio: nil)
salatLayl = Waterz(entitle: "Salat Al-Layl", artitle: "صلاة الليل", isFavorited: false, date: nil, frequency: nil, subMenus: [salatLaylMethod, salatLaylImportance], content: "salatLayl", location: nil, audio: nil)
prayers = Waterz(entitle: "Prayers", artitle: "الصلوات", isFavorited: nil, date: nil, frequency: nil, subMenus: [salatGhufayla, salatLayl], content: nil, location: nil, audio: nil)
amal = Waterz(entitle: "A'mal", artitle: "الأعمال", isFavorited: nil, date: nil, frequency: nil, subMenus: [prayers], content: nil, location: nil, audio: nil)
// more variables initialized here
// MARK: - Favorites
favorites = Waterz(entitle: "Favorites", artitle: "المفضلة", isFavorited: nil, date: nil, frequency: nil, subMenus: nil, content: nil, location: nil, audio: nil)
mainCategories = Waterz(entitle: "Categories", artitle: "الأقسام", isFavorited: nil, date: nil, frequency: nil, subMenus: [duas, amal, ziyarat, munajat, taqeebat, others, favorites], content: nil, location: nil, audio: nil)
}
}
我的第一个问题是,如何遍历数据库内的所有变量以查看是否isFavorited = true
,然后将该项目附加到"收藏夹"的"子菜单"中?有没有办法做到这一点,或者我应该将收藏夹作为其他类别之外的单独类别? 接下来,由于我要在一周和日期的某些日子分配许多子类别(和子子类别等),我将如何在该特定日期提取变量?例如,如果今天是星期二,并且总共有 5 个变量被分配了"星期二"这一天,那么它们都应该在控制台上打印。
最后一个问题:我有点迷茫,因为我不确定我是否正确地做了我的数据库,也没有人可以问。我只是想看看我所做的是否真的正确。我所做的是陈述变量,然后在 init() 中赋予它的属性。这是对的吗?我必须再输入 2000 个变量。
这是我的应用程序的外观: 应用演示
使用过滤器:
// returns only favorite duas
let favorites = duas.filter { $0.isFavorited == true }
或者,您可以从子类别中筛选以获取所需的内容。例如:
// Returns all duas where it contains `Tuesday` in the `frequency`
subcategories.filter { $0.frequency.contains(Tuesday) }