Ambigious reference to member request() issues with Alamofire after migration to swift 3

99封情书 提交于 2019-12-10 03:54:20

问题


So I am new to swift as a whole so bear with me.. I had the query working in swift 2 but after I migrated to swift 3 I get the error

Ambiguous reference to member 'request(_:withMethod:parameters:encoding:headers:)'

Below is the code in which it occurs:

Alamofire.request(.GET, serverRequest).validate().responseJSON( { response in
        switch response.result{
        case .Success:
            if let JSON = response.result.value {
                let final = JSON[wantedClass] as! String//forces output to string
                self.failure("You asked what your " + wantedClass + " is, it is:", message: final)
            }
        case .Failure(let error):
            print(error)
        }
})

I am at a loss of what is the problem.


回答1:


I have Alamofire swift3 branch working in Xcode 8.0 ß6 with the following code:

Alamofire.request("https://\(ip)/api/version", withMethod: .get, 
    parameters: nil, encoding: .json, headers: headers)
            .validate()
            .responseJSON { response in
                //debugPrint(response)
                switch response.result {
                case .success:
                    if let JSON = response.result.value {
                        let version = Mapper<Version>().map(JSON)
                        print("Version \(version?.server!)")
                    }
                case .failure(let error):
                    print (error)
                }
            }

Pay close attention to the order and types of your arguments to .request

You should have only one Alamofire framework active. Try to redo it in another clone, or maybe try the following in the clone you have?

pod cache clean --all
pod install

What does your Podfile have in it?

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

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



回答2:


I had the same problem - they reordered parameters in the method, so now '.get' should be placed after withMethod.

If you use Alamofire 4.0 in your project (I assume you do, because this error I got on version 4) you need to write

Alamofire.request(serverRequest, withMethod: .get)

or for example

Alamofire.request(serverRequest, withMethod: .get, parameters: nil, encoding: .url, headers: nil)


来源:https://stackoverflow.com/questions/39029890/ambigious-reference-to-member-request-issues-with-alamofire-after-migration-to

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