【问题标题】:Swift 4.2 - Alamofire all of a sudden not workingSwift 4.2 - Alamofire 突然不工作了
【发布时间】:2019-03-16 00:24:14
【问题描述】:

我将 Alamofire 4.7 与 Swift 4.2 一起使用,自从将我的代码转换为 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) 但我的帖子是空的。我做错了什么?

【问题讨论】:

  • 您是否尝试过将您的 URL 放入 Postman 之类的内容中,以查看您访问的 API 是否有任何反作用?
  • 是的,我做到了......我使用了 Postman,它返回了预期的结果。
  • 另一个想法是清理缓存(可能关闭 Xcode,从命令行 nuke 派生数据)并重新打开项目。另一个想法是更新你的 CocoaPods/Carthage/whatever 以防自动 4.2 Swift 更新发生一些奇怪的事情。最新的Alamofire is 4.7.3 看起来像是 Swift 4.2 更新。
  • 你试过转换后重启你的笔记本电脑吗?
  • 你没有在后端获取任何参数,这是由客户端发送的。是你面临的问题吗?

标签: ios swift alamofire


【解决方案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")
        }
    }

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 2018-01-19
    • 1970-01-01
    • 2019-11-09
    • 2017-11-07
    • 2014-04-16
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多