Alamofire Swift 3.0 dodatkowy argument w wywołaniu

Przeniosłem swój projekt do Swift 3 (i zaktualizowałem Alamofire do najnowszej wersji Swift 3 za pomocą pod 'Alamofire', '~> 4.0' w pliku Podfile).

Dostaję teraz błąd "dodatkowy argument w wywołaniu" na każdym Alamofire.Prośba. Eg:

let patientIdUrl = baseUrl + nextPatientIdUrl
Alamofire.request(.POST, patientIdUrl, parameters: nil, headers: nil, encoding: .JSON)
Czy ktoś może mi powiedzieć dlaczego ?
Author: Matt Hampel, 2016-09-14

14 answers

Zgodnie z alamofire dokumentacja dla wersji 4.0.0 żądanie adresu URL z http metoda będzie następująca:

Alamofire.request("https://httpbin.org/get") // method defaults to `.get`    
Alamofire.request("https://httpbin.org/post", method: .post)
Alamofire.request("https://httpbin.org/put", method: .put)
Alamofire.request("https://httpbin.org/delete", method: .delete)

Więc twoje żądanie url będzie:

Alamofire.request(patientIdUrl, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil)

I przykładowe żądanie będzie:

Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: [AUTH_TOKEN_KEY : AUTH_TOKEN])
    .responseJSON { response in
        print(response.request as Any)  // original URL request
        print(response.response as Any) // URL response
        print(response.result.value as Any)   // result of response serialization
}
Mam nadzieję, że to pomoże!
 74
Author: Abdullah Md. Zubair,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-04 09:28:05

To mi pomogło.
nie ma potrzeby usuwania kodowania parametru

Swift 3.x / 4.x

Alamofire.request("https://yourServiceURL.com", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)
            }
            break

        case .failure(_):
            print(response.result.error)
            break

        }
    }

I upewnij się, że parametry są typu

[String:Any]?

W przypadku Get

Alamofire.request("https://yourGetURL.com", method: .get, parameters: ["":""], encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)
            }
            break

        case .failure(_):
            print(response.result.error)
            break

        }
    }

Działa nawet z

JSONEncoding.default 

Dla Nagłówków

Jeśli przekazujesz nagłówki, upewnij się, że ich typ powinien być [String:String]

Przejdź przez Parameter Encoding Link https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol

 66
Author: Rajan Maheshwari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-22 12:44:32

Post method Alamofire 4.0 with Swift 3.0 and xCode 8.0

Alamofire.request(URL, method: .post, parameters: PARAMS)
                            .responseJSON { closureResponse in
                        if String(describing: closureResponse.result) == "SUCCESS"
                        { 
                           // Sucess code  
                        }
                        else
                        { 
                           // Failure Code 
                        }
                 }
 5
Author: Mohammad Kamran Usmani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-29 09:43:19

Moim rozwiązaniem jest to, że jeśli używasz nagłówków, jego typem musi być [String: String].

 4
Author: xevser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-09 14:20:59

Właśnie rozwiązałem ten sam problem co Ty. Problem polega na tym, że zaimportowałem Alamofire w nagłówku, więc po prostu usuwam Alamofire podczas żądania połączenia. Tak:

Request(.POST, patientIdUrl, parametry: nil, nagłówki: nil, kodowanie: .JSON)

Mam nadzieję, że ci pomoże.

 1
Author: Chi Minh Trinh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-27 01:54:52

Ten błąd zależy od wartości parametrów. Musi być [String: String]

let url = URL(string: "http://yourURLhere")!

    let params: [String: String] = ["name": "oskarko", "email": "[email protected]", "sex": "male"]



    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).validate(statusCode: 200..<600).responseJSON() { response in

        switch response.result {
        case .success:

            var result = [String:String]()

            if let value = response.result.value {

                let json = JSON(value) 

            }

        case .failure(let error):
            print("RESPONSE ERROR: \(error)")

        }

    }
 1
Author: oskarko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-24 08:25:35

Natknąłem się na ten sam dodatkowy argument 'metoda' w wywołaniu błąd, gdy moja zmienna URL była poza zakresem.

W Twoim przypadku upewnij się, że zarówno baseUrl, jak i nextPatientIdUrl są objęte zakresem stosowania metody Alamofire.request(patientIdUrl,..).

Mam nadzieję, że to rozwiąże twój problem. Dziękuję!
 1
Author: onlinebaba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-27 08:54:49

Dla mnie to działa.

Dla żądania GET

Alamofire.request("http://jsonplaceholder.typicode.com/todos/1/get").responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if response.result.value != nil{
                print(response.result.value!)
            }
            break

        case .failure(_):
            print(response.result.error)
            break

        }

    }

For POST

let parameters = NSDictionary(object: "nara", forKey: "simha" as NSCopying)

    Alamofire.request("http://jsonplaceholder.typicode.com/posts", method: HTTPMethod.post, parameters: parameters as? Parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in


        switch(response.result) {
        case .success(_):
            if response.result.value != nil{
                print(response.result.value!)
            }
            break

        case .failure(_):
            print(response.result.error)
            break

        }
    }

Dzięki @ Rajan Maheswari .

 0
Author: Narasimha Nallamsetty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 09:29:02

Naprawiłem ten problem za pomocą:

  1. Zmień kolejność parametrów (url, następnie Typ metody).
  2. Zmień Enum kodowania na " JSONEncoding.default " na przykład.

Zauważ, że: zmiana podpisu metod Alamofire w Swift 3

 0
Author: Ahmed Lotfy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-03 08:14:13

Dwie rzeczy, które uważam za warte odnotowania.

  1. usuń pierwszą Etykietę url przed jej wartością. Użycie Alamofire.request("https://yourServiceURL.com", method: .post, zamiast Alamofire.request(url: "https://yourServiceURL.com", method: .post,.
  2. Upewnij się, że typ danych parametrów to [String: String]. Zadeklaruj to wyraźnie.
 0
Author: Jiang Xiang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-02 23:09:28

Kopiuję ten kod z Alamofire, tworzę URLRequest i używam Alamofire.metoda request (URLRequest), unikaj tego błędu

originalRequest = try URLRequest(url: url, method: method, headers: headers)
let encodedURLRequest = try encoding.encode(originalRequest!, with: parameters)
 0
Author: Arthur Liu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-02 10:50:48

Naprawiłem ten problem w ten sposób:

Po Prostu Usuń Dodatkowe parametry, po prostu parameters, encoding i headers, jeśli te parametry są zerowe możesz usunąć then I zostawić w ten sposób,

Alamofire.request(yourURLString, method: .post)
 0
Author: Vinicius Carvalho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-04 01:41:19
func API()
{
    if Reachability.isConnectedToNetwork()
    {
        let headers = ["Vauthtoken":"Bearer \(apiToken)"]
        print(headers)
        //            let parameter = ["iLimit":"10","iOffset":"0","iThreadId":"1"]
        ApiUtillity.sharedInstance.showSVProgressHUD(text: "Loding...")
        Alamofire.request(ApiUtillity.sharedInstance.API(Join: "vehicle/CurrentVehicleLists"), method:.get, parameters:nil, headers: headers).responseJSON { response in
            switch response.result {
            case .success:
                print(response)
                ApiUtillity.sharedInstance.dismissSVProgressHUD()
                let dictVal = response.result.value
                let dictMain:NSDictionary = dictVal as! NSDictionary
                let statusCode = dictMain.value(forKey: "status") as! Int
                if(statusCode == 200)
                {

                }
                else if statusCode == 401
                {

                }
                else
                {

                }
            case .failure(let error):
                print(error)
                ApiUtillity.sharedInstance.dismissSVProgressHUD()
            }
        }
    } else
    {
        ApiUtillity.sharedInstance.dismissSVProgressHUD()
        ApiUtillity.sharedInstance.showErrorMessage(Title: "Internet Connection", SubTitle: "Internet connection Faild", ForNavigation: self.navigationController!)
    }
}
 0
Author: jignesh kasundra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-17 12:27:45

Jeśli dodałeś pliki alamofire lokalnie, nie używaj "Alamofire" przed żądaniem

let apipath = “your api URL”    
    request(apipath, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { response in switch(response.result) {
            case .success(_):
                do {
                    let JSON = try JSONSerialization.jsonObject(with: response.data! as Data, options:JSONSerialization.ReadingOptions(rawValue: 0))

                    guard let JSONDictionary: NSDictionary = JSON as? NSDictionary else {
                        print("Not a Dictionary")
                        return
                    }

                    print("Post Response : \(JSONDictionary)")
                }
                catch let JSONError as NSError {
                    print("\(JSONError)")
                }
                break
            case .failure(_):
                print("failure Http: \(String(describing: response.result.error?.localizedDescription))")
                break
            }
    }
 0
Author: Pritesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-14 12:22:46