代码之家  ›  专栏  ›  技术社区  ›  user979331

斯威夫特4.2-阿拉莫菲突然不工作了

  •  0
  • user979331  · 技术社区  · 6 年前

    我正在使用Alamofire4.7和SWIFT4.2,从那时起,将我的代码转换成SWIFT4.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)!)
                }
    
            }
        }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   MarkCoolski    6 年前

    你知道用swift4.2可以非常容易地解析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")
            }
        }