swift - Alamofire 4 + 模型返回一个空值

标签 swift alamofire

我仍是 iOS 开发的初学者。我正在使用 Alamofire 4 和 SwiftyJSON 来获取数据。当我调试时,我注意到它首先执行 return country_list ,然后只去 Alamofire body。我做了一些搜索,但仍未找到答案。

模型

import UIKit
import SwiftyJSON
import Alamofire

class CountryList {

    var ReturnOK: String?
    var Countryid: String?
    var Countryname: String?

    init(ReturnOK: String, Countryid: String, Countryname: String) {
        self.ReturnOK = ReturnOK
        self.Countryid = Countryid
        self.Countryname = Countryname
    }

    /*
     *  Fetch the country list
     */
    class func fetchCountryList() -> [CountryList] {

        var country_list = [CountryList]()

        // Add parameters
        let param: [String: String] = [
            Constants.LIST_COUNTRY.strAppID: Constants.OTHERS.temp_strAppID
        ]

            Alamofire.request(Constants.LIST_COUNTRY.URL, parameters:param, encoding: URLEncoding.default).responseJSON { (response) in
                switch response.result {

                case .success(let value):
                    let json = JSON(value)

                    for (_, subJson):(String, JSON) in json {

                        let item = CountryList(
                            ReturnOK: subJson["ReturnOK"].stringValue,
                            Countryid: subJson["Countryid"].stringValue,
                            Countryname: subJson["Countryname"].stringValue)

                        country_list.append(item)
                    }

                case .failure(let error):
                    print(error)
                }

            }

        return country_list
    }

}

View Controller

class CreateAccountViewController: UIViewController, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

    @IBOutlet weak var tf_country: UITextField!

    @IBOutlet weak var picker_country: UIPickerView!

    var countryL: [CountryList]?

    override func viewDidLoad() {
        super.viewDidLoad()
        //  Get the country list
        countryL = CountryList.fetchCountryList()
        print(countryL)
    }

func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == UIColor.lightGray {
            textView.text = nil
            textView.textColor = UIColor.black
        }
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textView.text = "Address"
            textView.textColor = UIColor.lightGray
        }
    }

    //  Sets number of columns in picker view
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    //  Sets the number of rows in the picker view
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if let countryL = countryL {
            return countryL.count
        } else {
            return 0
        }
    }

    //  This function sets the text of the picker view to the content of the "salutations" array
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let titleRow = (countryL?[row] as? String)!
        return titleRow
    }

    // When user selects an option, this function will set the text of the text field to reflect
    // the selected option.
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if (countryL?.count)! > 0 && (countryL?.count)! >= 0 {
            self.tf_country.text = self.countryL?[row] as! String
            self.picker_country.isHidden = true
        }
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        //  When select th texfield, begin editing
        if (textField == self.tf_country) {
            self.picker_country.isHidden = false
            self.view.endEditing(true)
        }
    }
}

在模型上我可以看到数据是追加的。但是当我打印时,它是零。

非常感谢任何帮助。

最佳答案

你应该用 Completion Handler 创建一个函数

这里的想法是你的 Alamofire 函数是异步的,这意味着,当你调用 return country_list 时, Alamofire 请求还没有完成.

你应该创建一个带有handler的函数

class func fetchCountries(handler: @escaping ([CountryList]) -> Void)) {
    //alamofire request here
    ...
    case .success(let value):
        let json = JSON(value)

        for (_, subJson):(String, JSON) in json {

            let item = CountryList(
                ReturnOK: subJson["ReturnOK"].stringValue,
                Countryid: subJson["Countryid"].stringValue,
                Countryname: subJson["Countryname"].stringValue)

             country_list.append(item)
         }
         handler(countryList) //this return countryList from this functions
} 

在你的 UIViewController 里面它看起来像

override func viewDidLoad() {
    super.viewDidLoad()
    //  Get the country list
    CountryList.fetchCountryList { [weak self] countryList in
        guard let `self` = self else { //this here to avoid reference cycle
          return
        }
        self.countryL = countryList
        //update your UI here
        print(countryL)
    }
}

关于swift - Alamofire 4 + 模型返回一个空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43177257/

相关文章:

swift - 缩放和置换节点的最佳方法是什么?

ios - 使用 OHHTTPStubs 进行模拟时未到达 Alamofire 响应 block

ios - 为 Alamofire 创建请求主体

ios - Alamofire自定义参数

ios - NSTemporaryDirectory 中的 Alamofire MultiPartForm 文件

ios - 如何使用Swift 3.0在Alamofire区 block 之外访问数据

ios - 删除单元格后更新标签

ios - UIView animateWithDuration 太快了

swift - ios-charts xaxis 标签未对齐

ios - 当字体变大时如何将一个约束优先于另一个约束