1

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.

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32

1 Answers1

0

Rather than relying on NSNull mapping correctly through various abstractions between Alamofire and the data being sent over HTTP with your parameters, I would do the following:

func formatTime(time: Date?) -> String? {
  guard let time = time else { return nil }
  return formatter.string(from: time)
}

var parameters: [String: Any] = [:]
  for week in days {
    if let hoursStart = formatTime(time: week.hoursStart) { parameters["\(week.day)_hours_start"] = hoursStart } 
    if let hoursEnd = formatTime(time: week.hoursEnd) { parameters["\(week.day)_hours_stop"] = hoursEnd } 
    if let lunchStart = formatTime(time: week.lunchStart) { parameters["\(week.day)_lunch_hours_start"] =  lunchStart } 
    if let lunchStop = formatTime(time: week.lunchEnd) { parameters["\(week.day)_lunch_hours_stop"] = lunchStop } 
    if let breakStart = formatTime(time: week.breakStart) { parameters["\(week.day)_break_hours_start"] = breakHoursStart } 
    if let breakEnd = formatTime(time: week.breakEnd) { parameters["\(week.day)_break_hours_stop"] = breakEnd }  
  }

As an aside, your question should state where exactly you are seeing <null> as opposed to null? In some logging call in Cocoa, or at your server end? Again though, safer (and arguably Swiftier, as formatTime has a meaningful return type in above example) option would be above IMO.

mz2
  • 4,672
  • 1
  • 27
  • 47