Alamorafire + SwiftyJson + FacebookSDK

I am using FBSDK with Xcode 7.2.1 to extract user profile information and use SwiftyJson to process and process json data. After changing the values ​​/ deleting some, I want to make a request to send to api with JSON data. But I quickly got stuck in problems with a very bad type.

This is my code to create a mail 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 } 

Sorry, I get this error:

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

If I try to send JSON data directly to the API, I get this error:

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

Any help is appreciated. I just want to know how to convert a JSON object to a String AnyObject? without crashing. Or send a Json object as the post / put / patch request data using Swift 2.

+8
xcode swift facebook-ios-sdk
source share
1 answer

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


Expand the optional dictionary with 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 } } 
+1
source share

All Articles