Search code examples
swiftxcodeswift3alamofire

Swift3/Alamofire4 migration, having to make all elements 'as AnyObject' for [String: AnyObject]


I have old code from the previous Swift/Alamofire version that worked fine before

parameters = ["name":name, "description":description, "location":location, "start_time":start_time , "end_time":end_time/* ..etc */] as [String: AnyObject]

Alamofire.request(.PUT, url, parameters: parameters, encoding: .json, headers: ["Authorization": auth_token, AppVersionKey: AppVersionValue]).validate().responseJSON() { response in

    print("Status code of default pic call: \(response.response?.statusCode)")
            }

I know I now have to switch it to

Alamofire.request(url, method: .put, ... , encoding: JSONEncoding.default ...)

But I'm getting Xcode errors for parameters and the fix-it is just adding as AnyObject to each value in the k,v pairs which then results in an "Expression was too complex to be solved in reasonable time ..." error. How am I supposed to do this with the new Swift3 and Alamofire4? Not even sure if this is a Swift error or Alamofire migration issue


Solution

  • You're assigning to parameters with a literal of type [String: Any] cast to a [String: AnyObject] which is why the compiler is complaining.

    If you remove the cast at the end of the assignment and update the type of parameters to [String: Any] it should work.

    This is due to a Swift 3 change which basically replaced many former uses of AnyObject with Any so that structs and enums would also be accepted.