【问题标题】:Alamofire download from JSON API从 JSON API 下载 Alamofire
【发布时间】:2017-02-20 19:13:46
【问题描述】:

我正在尝试在 api 的标签上设置文本,但似乎该函数甚至没有被调用。请参考下面的sn-p。有什么问题吗?

编辑:typealias DownloadComplete = () -> ()

var date: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    timeLbl.text = date

    // Do any additional setup after loading the view.
}

func downloadTimeData(completed: @escaping DownloadComplete) {
    //Downloading forecast weather data for TableView
    Alamofire.request(APIURL).responseJSON { response in

        let result = response.result

        if let dict = result.value as? Dictionary<String, AnyObject> {
            if let currentDate = dict["fulldate"] as? String {
                self.date = currentDate
                print(self.date)
                print("xxx")
            }
        }
     completed()
    }
}

【问题讨论】:

    标签: json swift function api alamofire


    【解决方案1】:

    我通过 alamofire 文档以更简单、更容易的方式解决了这个问题。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        Alamofire.request(APIURL).responseJSON { response in
            print(response.result)   // result of response serialization
            let result = response.result
    
            if let dict = result.value as? Dictionary<String, AnyObject> {
                let currentDate = dict["fulldate"] as? String
                self.timeLbl.text = currentDate
                }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      在您发布的代码中,您没有在任何地方调用downloadTimeData(completed:)

      您可以在viewDidAppear(_:) 中执行此操作,例如:

      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
      
          downloadTimeData { 
              // The request has completed
              timeLbl.text = date
          }
      }
      

      请注意,您可能需要稍微更改调用,具体取决于DownloadComplete 的定义方式。

      【讨论】:

        【解决方案3】:

        您在 viewDidLoad 中的页面加载时立即设置 timeLbl.text,但您没有告诉应用执行其他任何操作。

        您必须将 downloadTimeData 移动到 viewDidLoad,并在完成时设置 'timeLbl.text = date'

        您必须在拨打电话时设置某种文本占位符或加载程序,因为您不能保证它是即时的。

        我们是在设置一个标签吗?还是标签的整个表格视图?

        我把一些语法改成了“swiftier”

        var date = ""
        
        override func viewDidLoad() {
          super.viewDidLoad()
          //call downloadTimeData here
          downloadTimeData() {
            //once we are in completion, this means internet call finished, so set label now
            self.timeLbl.text = date
          }
        }
        
        func downloadTimeData(completed: @escaping DownloadComplete) {
        //Downloading forecast weather data for TableView
          Alamofire.request(APIURL).responseJSON { response in
            guard let dict = response.result.value as? [String: AnyObject], let currentDate = dict["full date"] as? String else {
              //handle error here if response fails to give you good data
              completed()
              return
            }
            self.date = currentDate
            print(self.date)
            print("xxx")
            completed()
          }
        }
        

        【讨论】:

        • 我将其发布到 DownloadComplete 只是为了确保一切都清楚。
        猜你喜欢
        • 1970-01-01
        • 2019-10-06
        • 1970-01-01
        • 2021-07-08
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多