2017-08-27 77 views
0

我使用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") 
     } 
    } 

} 

Running result

我斯威夫特3的初学者和网络预设电台,我谷歌,看到斯威夫特文档不知道如何解决这个问题。任何人都可以帮忙?非常感谢你。

+0

关闭闭包有返回类型void。你不能从闭包返回,而是使用完成处理程序。 –

回答

2

您没有使用这就是为什么getLastModifiedDate返回nil,因为它没有返回"Last-Modified"值。你必须使用闭包,如果你正在做异步任务这样

func getLastModifiedDate(completion: @escaping (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 
      completion(date) 

    } 

} 
+0

因为我想提示alert view和用户点击OK,我会调用一个下载json(callDowload)函数。我计划callDownload也会有返回值,它是否也使用callDownload()中的closure?我可以不使用异步模式来等待服务器返回,然后执行下一步吗?我的观念错了吗? –

+0

看到,调用下载正在执行服务器调用,因此您必须等待它,因此您必须使用完成处理程序来等待响应。所以如果你执行服务器调用,那么你必须使用闭包。希望你有? –

0

试试这个代码

func getLastModifiedDate() -> String? { 

    var date:String? 

    Alamofire.request("http://www.example.com/apps/demopad/manifest.json") 
     .response { 
      switch response.result { 
      case .success(let JSON): 
       print("Success with JSON: \(JSON)") 
       print("Request failed with error: \(response.response?.statusCode)") 

       if let response = JSON as? NSMutableDictionary 
       { 

        date = response?.object(forKey: "Last-Modified") as! String 

        print(date ?? "Server date is nil") 
        self.lastModifedDateLabel.text = date 
       } 
       else if let response = JSON as? NSDictionary 
       { 

        date = response?.object(forKey: "Last-Modified") as! String 

        print(date ?? "Server date is nil") 
        self.lastModifedDateLabel.text = date 
       } 
       else if JSON is NSArray 
       { 

       } 

       break 

      case .failure(let error): 
       print("Request failed with error: \(error)") 
       callback(response.result.value as? NSMutableDictionary,error as NSError?) 
       break 
      } 


     } 
     .responseString { response in 

      print("Response String: \(response.result.value)") 
    } 
} 
return date 
}