Reputation: 11841
I am using Alamofire 4.7 with Swift 4.2 and ever since converting my code to Swift 4.2 Alamofire all of sudden does not work at all.
I have a simple call like so:
func createUser(username: String, email: String, password: String, passwordConfirm: String, completion: @escaping (_ result: String) -> Void)
{
let parameters: Parameters = [
"username" : username,
"email" : email,
"password" : password,
"confirm_password" : passwordConfirm
]
Alamofire.request(webservice + "?action=register", method: HTTPMethod.post, parameters: parameters, encoding: URLEncoding.httpBody, headers: [:]).responseJSON { response in
if(response.error == nil)
{
if let result = response.result.value {
let jsonData = result as! NSDictionary
if(jsonData["response"] == nil)
{
completion("")
}
else
{
completion(jsonData["response"] as! String)
}
}
}
else
{
completion((response.error?.localizedDescription)!)
}
}
}
All the parameter are getting populated properly, after checking my api, its call the correct method (?action=register) but my post is empty. What am I doing wrong?
Upvotes: 0
Views: 1012
Reputation: 88
Did you know you can parse JSON super easily with Swift 4.2? I used it recently with a dialling code table view - the data was a local JSON file:
struct Countries: Codable {
let countries: [Country]
}
struct Country: Codable {
let code: Int
let name: String
let flagImage: String
}
enum CodingKeys: String, CodingKey {
case code
case name
case flagImage
}
class CountryListVC: UITableViewController {
func loadJSON() {
if let path = Bundle.main.path(forResource: "countryDiallingCodes", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = try JSONDecoder().decode(Countries.self, from: data)
print("JSON Object: ", jsonObj)
countries = jsonObj.countries
} catch let error {
print (error)
}
} else {
print ("Error in path")
}
}
Upvotes: 1