无法将元素添加到数组中

时间:2017-07-06 10:33:48

标签: ios arrays json swift alamofire

我是Swift的新手,我遇到了向数组添加元素的问题

我试图将6个对象从JSON存储到Array。但数组是零。

我在调试时看到它添加到Array,我不知道为什么在完成请求JSON后它为零?

JSON:

[
    {
        "amount": "20.0",
        "date": "10/07/2016"
    },
    {
        "amount": "21.0",
        "date": "11/07/2016"
    },
    {
        "amount": "16.0",
        "date": "12/07/2016"
    },
    {
        "amount": "30.0",
        "date": "13/07/2016"
    },
    {
        "amount": "50.0",
        "date": "14/07/2016"
    },
    {
        "amount": "33.0",
        "date": "15/07/2016"
    }
]

服务

    class DownloadService {
    static let instance = DownloadService()
    //static var goldArray = [Gold]()
    var goldArray = [Gold]()
    func getData() {
        Alamofire.request("https://rth-recruitment.herokuapp.com/api/prices/chart_data", headers: headers).responseJSON { response in
            if let objJson = response.result.value as! NSArray?{
                for element in objJson {
                    let data = element as! NSDictionary
                    let amount = data["amount"] as! String
                    let date = data["date"] as! String
                    let gold = Gold(amount: amount, date: date)
                    self.goldArray.append(gold)
                    print(self.goldArray.count)//print 1 to 6
                }
            }
        }
        printGoldArray()
    }

    func printGoldArray(){
        for element in self.goldArray {
            print(element)//print nothing
        }
    }
}

金级:

    class Gold{
    let amount:String
    let date: String

    init(amount: String, date: String) {
        self.amount = amount
        self.date = date
    }

}

的ViewController:

    class ViewController: UIViewController {
    //var dataArray = [Gold]()
    override func viewDidLoad() {
        super.viewDidLoad()
        DownloadService.instance.getData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

3 个答案:

答案 0 :(得分:1)

这里从闭包外部调用printGoldArray函数,这意味着当调用printGoldArray时,goldArray可能没有任何值。

尝试在添加值后从闭包内部调用printGoldArray函数。请尝试以下代码

func getData() {
    Alamofire.request("https://rth-recruitment.herokuapp.com/api/prices/chart_data", headers: headers).responseJSON { response in
        if let objJson = response.result.value as! NSArray?{
            for element in objJson {
                let data = element as! NSDictionary
                let amount = data["amount"] as! String
                let date = data["date"] as! String
                let gold = Gold(amount: amount, date: date)
                self.goldArray.append(gold)
                print(self.goldArray.count)//print 1 to 6
            }
        }
     printGoldArray()
    }

}

答案 1 :(得分:0)

您需要在获得价值之前先解析您的JSON数据

do{
    let json = try JSONSerialization.jsonObject(with: response.result.value, options: []) as? [Any]
    let firstObj = json?[0] as! [String: String]
    let amount = firstObj["amount"]
    dump(amount)
}catch let error{

}

然后,在执行闭包之前,您的HTTP调用不会获得返回值。所以要打印你的数组,你需要在你的闭包内完成。例如,您可以在json解析后立即调用printGoldArray()

答案 2 :(得分:0)

首先,您的数组不是print数组元素,因为您在关闭后直接打印它。闭包使用异步范例。所以把打印功能放在封闭内。

并且第二件事是为什么使用强制完全打开。相反,您可以使用if letguard声明

你可以看到下面的代码。

func getData() {
        Alamofire.request("https://rth-recruitment.herokuapp.com/api/prices/chart_data", headers: headers).responseJSON { response in
            if let objJson = response.result.value as? NSArray {
                for element in objJson {
                    if let data = element as? NSDictionary {
                        let amount = data["amount"] as! String
                        let date = data["date"] as! String
                        let gold = Gold(amount: amount, date: date)
                        self.goldArray.append(gold)
                        print(self.goldArray.count)
                    }
                }
            }
            printGoldArray()
        }
    }