嗨,我正在为一个iOS快速项目使用TRON框架,我在为我的请求找到访问响应头的正确文档时遇到了一些困难。
对于后端,我使用Wordpress API v2,我想得到的是X总计和X TotalPages标题。
这是我的请求函数:
let tron = TRON(baseURL: "http://example.com/wp-json/wp/v2")
func getFeed(){
let request: APIRequest<JSONFeed,JSONError> = tron.request("/posts")
request.perform(withSuccess: {(response) in
print("Ready to parse")
self.articlesFeatured = (response.articles! as NSArray) as? [listingObject]
DispatchQueue.main.async {
self.collectionListing.reloadData()
}
}, failure: {(err) in
print("Error ", err)
})
}
以下是请求所用的方法:
class JSONFeed: JSONDecodable{
var articles: [listingObject]? = []
required init(json: JSON) throws{
let array = json.array
for articleJson in array! {
let article = listingObject()
let title = articleJson["title"].stringValue
article.title = title
self.articles?.append(article)
}
}
}
class JSONError: JSONDecodable{
required init(json:JSON) throws{
print("Got an Error")
}
}
请求和方法解析工作正常,但我只获得响应值,而没有其他信息,如头、状态代码等。
发布于 2017-05-11 12:39:08
可以使用performCollectingTimeline(withCompletion:)
方法,该方法在完成闭包中包含Alamofire.Response:
request.performCollectingTimeline(withCompletion: { response in
print(response.timeline)
print(response.result)
print(response.response)
})
发布于 2018-08-01 19:17:05
使用suhit的响应,我能够通过以下操作获得解决方案:
request.performCollectingTimeline { (response) in
response.response?.allHeaderFields.forEach({ (key, value) in
print("key: \(key) - Value: \(value)")
})
}
https://stackoverflow.com/questions/43909596
复制相似问题