How to use Observable Objects with this class?

188 views Asked by At

I'm trying to use Observable Objects with this class:-

    class malaysiaHolidays: ObservableObject
    {
        @Published var holidayName : String
        @Published var holidayDate : String
        
        init(holidayName: String, holidayDate: String)
        {
            self.holidayName = holidayName
            self.holidayDate = holidayDate
        }
        
    }

var holidayList = [malaysiaHolidays (holidayName: "Thaipusam", holidayDate: "2020-01-28"), malaysiaHolidays (holidayName: "Chinese New Year", holidayDate: "2020-02-12"), malaysiaHolidays (holidayName: "Chinese New Year", holidayDate: "2020-02-13"), malaysiaHolidays (holidayName: "Midterm School Break 1", holidayDate: "2020-03-13"), malaysiaHolidays (holidayName: "Sultan of Johor's birthday", holidayDate: "2020-03-23"), malaysiaHolidays (holidayName: "Awal Ramadan", holidayDate: "2020-04-13"), malaysiaHolidays (holidayName: "Labour Day", holidayDate: "2020-05-01"), malaysiaHolidays (holidayName: "Hari Raya Puasa", holidayDate: "2020-05-13"), malaysiaHolidays (holidayName: "Hari Raya Puasa", holidayDate: "2020-05-14"), malaysiaHolidays (holidayName: "Midyear School Break", holidayDate: "2020-05-22"), malaysiaHolidays (holidayName: "Vesak Day", holidayDate: "2020-05-26"), malaysiaHolidays (holidayName: "Agong's birthday", holidayDate: "2020-06-05"), malaysiaHolidays (holidayName: "Hari Raya Haji", holidayDate: "2020-07-20"), malaysiaHolidays (holidayName: "Midterm School Break 2", holidayDate: "2020-07-24"), malaysiaHolidays (holidayName: "National Day", holidayDate: "2020-08-31"), malaysiaHolidays (holidayName: "Awal Muharram (Maal Hijrah)", holidayDate: "2020-08-10"), malaysiaHolidays (holidayName: "Hari Malaysia", holidayDate: "2020-09-16"), malaysiaHolidays (holidayName: "Hari Hol Almarhum Sultan Iskandar", holidayDate: "2020-09-24"), malaysiaHolidays (holidayName: "Prophet Muhammad's birthday (Maulidur Rasul)", holidayDate: "2020-10-18"), malaysiaHolidays (holidayName: "Deepavali", holidayDate: "2020-11-04"), malaysiaHolidays (holidayName: "Year End School Break", holidayDate: "2020-11-20"), malaysiaHolidays (holidayName: "Christmas", holidayDate: "2020-12-25")]

The thing is, I'm not sure how to proceed at the ContentView

struct HolidayView: View
{
    @ObservedObject var finalHolidayList : ???    // <<<- here
    @ObservedObject var finalStateTitle : TheStateTitle = TheStateTitle(stateTitle: "Johor")
    

Can anyone help? Thanks.

1

There are 1 answers

0
Charles C. On

Your malaysiaHolidays seems like a struct that defines the structure.

struct malaysiaHoliday {
    var holidayName: String
    var holidayDate: String
}

The actual ObservableObject could be the HolidayList

class HolidayList: ObserableObject {
    @Published var holidayList: [malaysiaHolidays] = []
    ...
}

In your View:

struct HolidayView: View
{
    @ObservedObject var holidayList: HolidayList = ... <- you can init here

    ...
}