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.
Rather than relying on
NSNullmapping correctly through various abstractions between Alamofire and the data being sent over HTTP with your parameters, I would do the following:As an aside, your question should state where exactly you are seeing
<null>as opposed tonull? In some logging call in Cocoa, or at your server end? Again though, safer (and arguably Swiftier, asformatTimehas a meaningful return type in above example) option would be above IMO.