我使用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。你不能從閉包返回,而是使用完成處理程序。 –