class MainEventDTO: Mappable {
var id: Int
var title, contact, phone, createdAt, email: String
var day: String
var updatedAt: String
var events: [EventDTO]
var openReservation: Date
var closeReservation: Date
var reservationDateStatus: ReservationStatus
var crawlEpisodeNumber: String
var crawlEpisodeTitle: String
var crawlBody: String
init() {
id = 0
title = ""
contact = ""
phone = ""
email = ""
createdAt = ""
updatedAt = ""
events = []
day = ""
openReservation = Date()
closeReservation = Date()
reservationDateStatus = .closed
crawlEpisodeNumber = ""
crawlEpisodeTitle = ""
crawlBody = ""
}
required convenience init?(map: Map) {
guard map.JSON["id"] != nil, map.JSON["contact"] != nil, map.JSON["phone"] != nil, map.JSON["events"] != nil else { return nil }
self.init()
}
func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
contact <- map["contact"]
phone <- map["phone"]
email <- map["email"]
createdAt <- map["createdAt"]
updatedAt <- map["updatedAt"]
events <- map["events"]
day <- map["day"]
crawlEpisodeNumber <- map["crawlEpisodeNumber"]
crawlEpisodeTitle <- map["crawlEpisodeTitle"]
crawlBody <- map["crawlBody"]
openReservation <- (map["openReservation"], YearDateTransform())
closeReservation <- (map["closeReservation"], YearDateTransform())
reservationDateStatus = Date().reservationStatus(openReservation, closeReservation)
}
}
open class YearDateTransform: DateFormatterTransform {
static let reusableISODateFormatter = DateFormatter(withFormat: "yyyy-MM-dd", locale: Locale.current.identifier)
public init() {
super.init(dateFormatter: YearDateTransform.reusableISODateFormatter)
}
}
What you did:
let mainEvent = MainEvent() // Initialize and get all the info from my class
let repoString = mainEvent.toJSONString()
What you expected:
I expected something like:
getting my complete object parsed to json formatted string
What you got:
I get all the information correct, except for the openReservation and closeReservation its completely dissappeared.
Im using class, because im passing by reference, so thats needed.
I know its mapping the object properly, because if i print directly mainEvent.openReservation or closeReservation i get the date.... is just missing when converted to JSONString.... how can i fix this?
I need this because im storing it temporary in userdefaults as Cache information.
Your JSON dictionary:
Your model:
What you did:
What you expected:
I expected something like:
What you got: