我在Swift 4.2上使用Alamofire 4.7,自从将代码转换为Swift 4.2 Alamofire以来,突然之间根本无法正常工作。
我有一个简单的电话,像这样:
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)!)
}
}
}
检查我的api后,所有参数均正确填充,它调用了正确的方法(?action = register),但我的帖子为空。我在做什么错了?
答案 0 :(得分:1)
您知道您可以使用Swift 4.2轻松解析JSON吗? 我最近在拨号代码表视图中使用了它-数据是本地JSON文件:
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")
}
}