I have updated to Swift3 and the latest Alamofire '~> 4.0'
When I try and make my parameters I now get <null>
instead of null
for settings that I do not have set. Which conflicts with my backend.
Is there a way to send null
?
When I set the value to nil it is not included in the put
the dictionary strips it out. But I need all values to be sent back.
My code
func saveOfficeHours(days: [OfficeHours]) {
guard let URL = LocationInfo.shared.url else {
assertionFailure("location URL is nil")
return
}
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
func formatTime(time: Date?) -> Any {
guard let time = time else { return NSNull() }
return formatter.string(from: time)
}
var parameters: [String: Any] = [:]
for week in days {
parameters["\(week.day)_hours_start"] = formatTime(time: week.hoursStart)
parameters["\(week.day)_hours_stop"] = formatTime(time: week.hoursEnd)
parameters["\(week.day)_lunch_hours_start"] = formatTime(time: week.lunchStart)
parameters["\(week.day)_lunch_hours_stop"] = formatTime(time: week.lunchEnd)
parameters["\(week.day)_break_hours_start"] = formatTime(time: week.breakStart)
parameters["\(week.day)_break_hours_stop"] = formatTime(time: week.breakEnd)
}
Alamofire.request(URL, method: .put, parameters: parameters, encoding: URLEncoding.httpBody, headers: API.workstationTokenAuthHeaders()).validate().responseJSON {
response in
switch response.result {
case .success:
print("Success setting office hours")
case .failure(let error):
print("Failure setting office hours: \n \n -------==========-------- \n \n\(error)\n \n -------==========-------- \n")
}
}
}
Thanks for your help in advance.