我使用Alamofire框架来读取服务器json文件并下载它。我想检查json的最后修改日期,并让用户决定下载json的内容。但函数getLastModifiedDate总是返回零。我在这里列出示例代码。如何使用Alamofire Swift返回响应结果3
import Foundation
import UIKit
import Alamofire
class ViewController: UIViewController {
@IBOutlet var lastModifedDateLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (getLastModifiedDate() != nil) {
let alertController = UIAlertController(title: "Information", message: "Download or NOT", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "NO", style: .cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "YES", style: .default, handler: {action in
self.callDownload()
}))
self.present(alertController, animated: true, completion: nil)
}
}
func getLastModifiedDate() -> String? {
var date:String?
Alamofire.request("http://www.example.com/apps/demopad/manifest.json")
.response { serverResponse in
date = serverResponse.response?.allHeaderFields["Last-Modified"] as? String
print(date ?? "Server date is nil")
self.lastModifedDateLabel.text = date
}
return date
}
func callDownload() {
print("Call Download func")
Alamofire.request("http://www.example.com/apps/demopad/manifest.json")
.responseJSON { serverRespone in
let jsonDict = serverRespone.result.value as? Dictionary<String, Any>
print(jsonDict ?? "JSON data is nil")
}
}
}
我斯威夫特3的初学者和网络预设电台,我谷歌,看到斯威夫特文档不知道如何解决这个问题。任何人都可以帮忙?非常感谢你。
关闭闭包有返回类型void。你不能从闭包返回,而是使用完成处理程序。 –