Swift 3.0, Alamofire 4.0 Extra argument 'method' in call

…衆ロ難τιáo~ 提交于 2020-02-10 04:23:49

问题


I have read all the questions on this issue this and this. I have the following code

let fullURL = God.getFullURL(apiURL: self.apiUrl)
        if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .POST, AnyObject: self.postData?, encoding:.JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .GET, Parameters: getData, encoding:.JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

My Swift and Xcode versions are

Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
Version 8.2.1 (8C1002)

My pod file is

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target 'Buseeta' do
        pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'master'
end

pod 'AlamofireObjectMapper', '~> 4.0'

pod 'SwiftyJSON'

I get Extra argument 'method' in call error on both Alamofire request lines.

Dont go mark this question as duplicate without carefully checking. I have fixed the code exactly as per the duplicate questions.

EDIT 1

I tried after removing the headers, same issue on .POST and .GET

let fullURL = God.getFullURL(apiURL: self.apiUrl)
        if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .POST, AnyObject: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .GET, Parameters: getData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT 2

if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .post, parameters: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .get, parameters: getData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT 3

I replaced with method : HTTPMethod.get, still no change. Same issue.


回答1:


You need to use another function to upload data:

func upload(
    _ data: Data,
    to url: URLConvertible,
    method: HTTPMethod = .post,
    headers: HTTPHeaders? = nil)
    -> UploadRequest

And parameters for GET request must be of type [String: Any]




回答2:


The answer is in your link, here is the new definition of the method request:

let fullURL = God.getFullURL(apiURL: self.apiUrl)
        if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .post, parameters: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .get, parameters: getData, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        }



回答3:


What I tried was

var postData    : Parameters?
var getData    : Parameters?

in the init, I initalized the postData like this

self.postData       = try! JSONSerialization.data(withJSONObject: postData, options: JSONSerialization.WritingOptions.prettyPrinted)

and

 if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: HTTPMethod.post, parameters: postData as Parameters?, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method: HTTPMethod.get, parameters: getData, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT

This compiles without errors :). I tried the upload recommendation by @cosmicman66

if (getOrPost == God.POST) {
            Alamofire.upload(postData!, to: fullURL, headers:nil).responseJSON { response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method: HTTPMethod.get, parameters: getData, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT

This did not work. The request did not reach. I read somewhere that upload should not be used for POST.




回答4:


I got this error when I was passing an NSDictionary for parameters and not to mention the error message was misleading.

According to the documentation, the parameters of the request is of type [String: Any]. I got my issue fixed by doing so.

I’m on Xcode 8.3.1, Alamofire 4.4.0

This is the code that worked for me:

Alamofire.request(requestUrl, method: .post, parameters: requestParameters(), encoding: JSONEncoding.default, headers: nil)
            .responseJSON { response in

      print(response)
}

requestUrl is String, requestParameters() returns swift dictionary of type [String: Any]. And it is JSONEncoding.default, not .JSONEncoding.default

Refer this thread on GitHub for clarification:

https://github.com/Alamofire/Alamofire/issues/1508




回答5:


I am facing the same problem but by changing parameters type as [String : any] solve my problem

 let parameters : [String:any] = ["key":value]


来源:https://stackoverflow.com/questions/42113651/swift-3-0-alamofire-4-0-extra-argument-method-in-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!