我正在尝试将Observable Objects与以下类一起使用:-
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")]
问题是,我不确定如何在ContentView 上进行
struct HolidayView: View
{
@ObservedObject var finalHolidayList : ??? // <<<- here
@ObservedObject var finalStateTitle : TheStateTitle = TheStateTitle(stateTitle: "Johor")
有人能帮忙吗?谢谢
您的malaysiaHolidays
似乎是一个定义结构的结构。
struct malaysiaHoliday {
var holidayName: String
var holidayDate: String
}
实际的ObservableObject
可能是HolidayList
class HolidayList: ObserableObject {
@Published var holidayList: [malaysiaHolidays] = []
...
}
在您看来:
struct HolidayView: View
{
@ObservedObject var holidayList: HolidayList = ... <- you can init here
...
}