so I have been trying to create a "Period" class, with the following attributes:
class PTPeriod {
// MARK: Stored Properties
var start: Date
var end: Date
var place: PTPlace?
//MARK: DESIGNATED NITIALIZER
init(start: Date, end: Date, place: PTPlace? = nil) {
self.start = start
self.end = end
self.place = place
}
And I want to initialize it with a convenience init that accepts a dictionary like the following:
{
"close" : {
"day" : 1,
"time" : "0000"
},
"open" : {
"day" : 0,
"time" : "0900"
}
}
This is my initializer, originally I put the heavy lifting into a helper method. However, i was getting the same error I have now so I removed the helper method and the error is still occurring. Unsure where it thinks I am calling self.
UPDATE: moved the date formatting into a date formatter extension, but the error still persists! unsure WHY. new init shown:
My new Convenience init:
// MARK: Convenience init for initializing periods pulled from Google Place API
convenience init?(placeData: [String:Any], place: PTPlace? = nil) throws {
var start: Date? = nil
var end: Date? = nil
var formatter = DateFormatter()
do {
for (key, value) in placeData {
let period = value as! [String: Any]
if (key == "open") {
start = try formatter.upcoming_date(with: period)
} else if (key == "close") {
end = try formatter.upcoming_date(with: period)
} else {
break
}
}
if (start != nil && end != nil) { self.init(start: start!, end: end!, place: place) }
else { print("we f'd up") }
}
catch { print(error.localizedDescription) }
}
Here is my DateFormatter.upcoming_date method:
func upcoming_date(with googlePlacePeriod: [String: Any]) throws -> Date? {
if let day = googlePlacePeriod["day"] as? Int {
if (day < 0) || (day > 6) { throw SerializationError.invalid("day", day) } // throw an error if day is not between 0-6
if let time = googlePlacePeriod["time"] as? String {
if time.characters.count != 4 { throw SerializationError.invalid("time", time) } // throw an error if time is not 4 char long
var upcoming_date: Date
let gregorian = Calendar(identifier: .gregorian)
let current_day_of_week = Date().getDayOfWeekInt()
let dayOfPeriod = day + 1
///TRUST THAT THIS WORKS... NO JODAS
var distance = dayOfPeriod - current_day_of_week // inverse = false
if (dayOfPeriod < current_day_of_week) {
switch distance {
case -1: distance = 6
case -2: distance = 5
case -3: distance = 4
case -4: distance = 3
case -5: distance = 2
case -6: distance = 1
default: break
}
}
/** time example: "1535" translates into 3:35PM
first two characters in string are "hour" (15)
last two characters in string are "minute(35)
**/
let hour = Int(time.substring(to: time.index(time.startIndex, offsetBy: 2)))!
let minute = Int(time.substring(from: time.index(time.endIndex, offsetBy: -2)))
upcoming_date = Date().fastForward(amount: distance, unit: "days", inverse: false)!
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: upcoming_date)
components.hour = hour
components.minute = minute
upcoming_date = gregorian.date(from: components)!
return upcoming_date
}
else { throw SerializationError.missing("time") }
}
else { throw SerializationError.missing("day") }
}