2

This is a continuation of the question here

I have a class like this:

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
}

}

and another class that is my databank:

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)

}

}

My first question is, how do I loop through all the variables inside the databank to see if isFavorited = true, then I would append that item to the 'subMenus' of 'favorites'? Is there a way to do this or should I make favorites a separate category outside of the rest? Next, since I'm assigning many SubCategories (and SubSubCategories, and so on...) certain days of week and dates, how would I pull out the variable on that certain day? For example, if today is Tuesday and there are a total of 5 variables who are assigned the day "Tuesday", then they should all print on the console.

Final question: I'm kind of lost because I'm not sure if I'm doing my databank correctly and there's no one to ask. I just want to see if what I'm doing is actually correct. What I did was state the variable and then in the init() I gave it it's properties. Is that correct? I have to input like 2000 more variables.

This is how my app looks like: app demo

enigrify
  • 281
  • 3
  • 17

1 Answers1

1

Use filter:

// returns only favorite duas
let favorites = duas.filter { $0.isFavorited == true }

Or you can filter from subcategories to get what you need. For example:

// Returns all duas where it contains `Tuesday` in the `frequency`
subcategories.filter { $0.frequency.contains(Tuesday) }
  • 1
    Thank you for your answer. How would I do this if I just want to scan through all the variables in a class, and not an array? Thanks. – enigrify Aug 03 '19 at 17:45