Search code examples
swiftmappingalamofire

Swift 2 : How to downcast an Array of Objects


I have this Array and I am trying to POST it to backend, but got really confused with all the casting

valuesDictionary=["medication": Optional("Novocain"), "dateOfBirth": Optional(2001-    01-01 00:00:00 +0000), "lastName": Optional("Berthold"), "allergies":     Optional("Heuschnupfen"), "firstName": Optional("Alexander"), "Blutgruppe": Optional("A"), "PostalAddress": Optional(Eureka.PostalAddress(street: Optional("Gleimstraße"), state: nil, postalCode: Optional("10123"), city: Optional("Berlin"), country: Optional("DE")))]

trying to feed it into:

let request = Alamofire.request(.POST, Config.profileUpdate, parameters: valuesDictionary , encoding: .JSON)

I tried different things like:

let valuesDictionary = form.values() as! [String:AnyObject]

to downcast into the expected form but it just showing:

fatal error: can't unsafeBitCast between types of different sizes

Solution

  • Optionals aren't AnyObjects because Optional is an enum (a value type). You'll need to unwrap your optionals before you shove them in your dictionary.