在数组的append元素之后,它只在append方法中显示,当我们从另一个方法swift 3调用时显示为空



在数组的append元素之后,它只显示append方法,当我们从另一个方法调用swift 3时,它显示为空

进口UIKit进口Alamofire进口SwiftyJSON

类ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
let JSONUrl = "https://jsonplaceholder.typicode.com/posts"
var JSONData: JSON = nil
var JSONDataUserId = [Int]()
var JSONDataId = [Int]()
var JSONDataTitle = [String]()
var JSONDataBody = [String]()
override func viewDidLoad() {
    super.viewDidLoad()

    getDataFromJSON(url: JSONUrl)
    dispalyData()
}
func getDataFromJSON(url: String) {
    Alamofire.request(url).responseJSON { (response) in
        switch response.result{
        case .success(let data):
            self.JSONData = JSON(data)
            for i in 0..<self.JSONData.count{
                self.JSONDataUserId.append(self.JSONData[i]["userId"].intValue)
                self.JSONDataId.append(self.JSONData[i]["id"].intValue)
                self.JSONDataTitle.append(self.JSONData[i]["title"].stringValue)
                self.JSONDataBody.append(self.JSONData[i]["body"].stringValue)
            }
        case .failure(let error):
            print("Error Due to (error)")
        }
        print(self.JSONDataUserId)
    }
}
func displayData() {
    print(self.JSONDataUserId)
}

}

在上面的getDataFromJson方法中,语句print(self.JSONDataUserId)工作并显示数据,但是方法displayData print(self.JSONDataUserId)什么也不显示。我也想从displayData方法得到相同的结果

你正在使用异步代码块,所以试试这个。您正在发送服务器请求,然后并行调用getdatafrom server方法。更多信息请查看Apple Api Reference

    import UIKit
    import Alamofire 
    import SwiftyJSON
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    let JSONUrl = "https://jsonplaceholder.typicode.com/posts"
    var JSONData: JSON = nil
    var JSONDataUserId = [Int]()
    var JSONDataId = [Int]()
    var JSONDataTitle = [String]()
    var JSONDataBody = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()

        getDataFromJSON(url: JSONUrl)  { success, data in
             if success {
                for i in 0..< data.count{
                    self.JSONDataUserId.append(self.JSONData[i]["userId"].intValue)
                    self.JSONDataId.append(self.JSONData[i]["id"].intValue)
                    self.JSONDataTitle.append(self.JSONData[i]["title"].stringValue)
                    self.JSONDataBody.append(self.JSONData[i]["body"].stringValue)
                }
                dispalyData()
             }
        }
    }
    func getDataFromJSON(url: String, completionHandler: @escaping (Bool, JSON) -> ()) {
        Alamofire.request(url).responseJSON { (response) in
            switch response.result{
            case .success(let data):
                completionHandler(true, JSON(data))
            case .failure(let error):
                print("Error Due to (error)")
                completionHandler(false, nil)
            }
        }
    }
    func displayData() {
        print(self.JSONDataUserId)
    }
}

最新更新