简体   繁体   English

为什么我无法在Alamofire 4.0中使用SessionManager初始化或发出请求? (更新)

[英]Why can't I initialize or make a request with SessionManager in Alamofire 4.0? (UPDATED)

Migrating to Alamofire 4 and I am trying to initialize a session manager like so: 迁移到Alamofire 4 ,我正在尝试初始化会话管理器,如下所示:

let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 20
configuration.httpAdditionalHeaders = ["MyCompany-User-Agent": Config.MyCompanyUserAgentDataString]
var alamoManager: SessionManager = Alamofire.SessionManager(configuration: configuration)

However I keep getting the error as 但是我一直收到错误

Cannot invoke SessionManager with an argument list of type(configuration: URLSessionConfiguration) 无法使用参数列表类型(配置:URLSessionConfiguration)调用SessionManager

I am following the syntax at https://github.com/Alamofire/Alamofire#modifying-the-session-configuration , but mine has errors. 我遵循https://github.com/Alamofire/Alamofire#modifying-the-session-configuration的语法,但是我的有错误。 What am I doing wrong here? 我在这里做错了什么? Also I can't do a request from the SessionManager . 我也不能从SessionManager发出请求。

For example: 例如:

Alamofire.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default).responseObject { (response: DataResponse<MyCustomResponse>) in
            if let result = response.result.value {} }

Works with no issues, BUT (where alamoManager is a SessionManager): 没有问题, 但正常工作 (其中alamoManager是SessionManager):

alamoManager.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default).responseObject { (response: DataResponse<MyCustomResponse>) in
            if let result = response.result.value {} }

It doesn't work. 没用 I get the error as 我得到的错误

Value of type SessionManager has no member request 类型为SessionManager的值没有成员请求

Very confused as to why I am getting that error. 关于我为什么收到该错误的消息非常困惑。 The docs clearly show an example of a SessionManager object using a request method. 该文档清楚地显示了使用请求方法的SessionManager对象的示例。

I have noticed though that there are no issues with this code if I create a new project. 我注意到,如果我创建一个项目,则此代码没有任何问题。 However in my existing project these errors display. 但是,在我现有的项目中,将显示这些错误。 I have checked the Podfile.lock and the version of Alamofire is indeed 4.0. 我检查了Podfile.lock和版本Alamofire确实是4.0。 I have build settings to target iOS9.0 for all targets and the project. 我有针对所有目标和项目的目标iOS9.0的构建设置。

Could someone help me with this please? 有人可以帮我吗? Tearing my hair out a bit! 弄乱我的头发!

I Suggest you initialize the extra headers with Alamofire's default Headers: 我建议您使用Alamofire的默认标头初始化额外的标头:

    // Adding Headers:
var defaultHeaders = Alamofire.SessionManager.defaultHTTPHeaders
defaultHeaders["MyCompany-User-Agent"] = Config.MyCompanyUserAgentDataString
    // adding Configuration:
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = defaultHeaders 
configuration.timeoutIntervalForRequest = 20
var alamoManager: SessionManager = Alamofire.SessionManager(configuration: configuration)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM