2017-08-27 36 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 
}