具有自定义参数编码的Alamofire,可快速应用

编程入门 行业动态 更新时间:2024-10-15 16:22:34
本文介绍了具有自定义参数编码的Alamofire,可快速应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须在我的swift应用程序中从soap Web服务调用一些方法,所以我认为我应该使用自定义参数编码,但是当我为这种编码创建闭包时,似乎永远都不会被调用。我在做错什么吗?

I have to call some methods from the soap web service in my swift application, so I think I should use custom parameter encoding, but when I create closure for this encoding it seems never to be called. Am I doing something wrong?

这是我的代码:

let custom: (URLRequestConvertible, [String: AnyObject]?) -> (NSURLRequest, NSError?) = { (URLRequest, parameters) in let mutableURLRequest = URLRequest.URLRequest.mutableCopy() as NSMutableURLRequest mutableURLRequest.setValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type") mutableURLRequest.HTTPBody = body return (mutableURLRequest, nil) } Alamofire.request(.POST, WebServiceURLString, parameters: nil, encoding: .Custom(custom)).responseString { (request, response, data, error) -> Void in println(data) }

中的空

推荐答案

自定义闭包不是设计执行的,因为没有要编码的参数。他是从 Alamofire.swift源文件中摘录的代码:

The custom closure is not executed by design, because there are no parameters to encode. He're a code excerpt taken from Alamofire.swift source file:

if parameters == nil { return (URLRequest.URLRequest, nil) }

如您所见,可以通过传递空字典来消除这种情况:

As you can see, you can bybass this condition by passing an empty dictionary:

Alamofire.request(.POST, WebServiceURLString, parameters: Dictionary(), encoding: .Custom(custom))

自定义闭包现在将被执行。

更多推荐

具有自定义参数编码的Alamofire,可快速应用

本文发布于:2023-11-26 02:18:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   参数   快速   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!