8

I use FBSDK with Xcode 7.2.1 to retrieve user profile information and use SwiftyJson to handle and manipulate json data. After changing values / removing some i want to make a post request to my api with the JSON data. But i am stucked with the really bad type issues in swift.

This is my code to make a post request:

 let headers = [
      "Content-Type": "application/json"
 ]
 let userProfile = userProfile as? [String : AnyObject]
 Alamofire.request(.POST, "http://localhost:8888/api/profile", parameters: userProfile, headers: headers, encoding:ParameterEncoding.URL) .response { request, response, data, error in
      print(userProfile) //This prints nil
      print(response) //This prints api error for expecting data
 }

Unfortunately i receive this error:

Cast from 'JSON' to unrelated type '[String : AnyObject]' always fails

If i try to send the JSON data directly to API i receive this error:

Cannot convert value of type 'JSON' to expected argument type '[String : AnyObject]?'

Any help are appreciated. I just want to know, how to convert JSON object to String AnyObject? without failing. Or sending Json object as post / put / patch request data with Swift 2.

artuc
  • 913
  • 11
  • 20

1 Answers1

1

JSON is a custom type defined in SwiftyJson. Try using userProfile.dictionaryObject in your case to get the underlying swift Dictionary.


Unwrap the optional dictionary by this

if let userProfile = userProfile.dictionaryObject {
    Alamofire.request(.POST, "http://localhost:8888/api/profile", parameters: userProfile, headers: headers, encoding:ParameterEncoding.URL) .response { request, response, data, error in
          print(userProfile) //This prints nil
          print(response) //This prints api error for expecting data
    }
}
J.Wang
  • 1,136
  • 6
  • 12
  • Thanks for the answer but when i do that i can't get a valid json so it throws error from api. This is the output before/after: http://pastebin.com/i1JqgFec – artuc Mar 03 '16 at 04:37
  • @artuc Yep, because by default, `userProfile.dictionaryObject` will return an optional `Dictionary`. You'll have to unwrap it. – J.Wang Mar 03 '16 at 04:56
  • Thanks for the help @J.Wang unfortunately, when i do that, it returns an invalid JSON. Before dictionaryObject, everything loooks great but i cant send JSON type with alamofire (it's also ok for me if there is any other libraries where i can send it), but with this one, the dictionaryObject returns wrong JSON so, my server returns an error to me. – artuc Mar 03 '16 at 06:36
  • @artuc If you could `print(userProfile)` right before the `Alamofire.request` and show the result, that would be great. – J.Wang Mar 03 '16 at 06:47
  • this is part of data it prints. As you can see it's not valid json. ["last_name": Artuç, "app": { source = ios; version = "0.1"; }, "is_verified": 0, "verified": { email = 1; "email_celebrity" = 0; fb = 1; "fb_celebrity" = 0; phone = 0; }, – artuc Mar 03 '16 at 07:37
  • 1
    @artuc It's a swift `Dictionary`. It's fine when you give this to to `Alamofire.request`. It will convert this into a valid JSON before sending to your server. There must be something else that causes the error? Can you double check it? – J.Wang Mar 03 '16 at 09:32
  • you are awesome. I always thought what i see is what i send. Thank you. – artuc Mar 03 '16 at 10:36