如何在Swift 4中访问全局数组? [英] How to access global array in Swift 4?

查看:78
本文介绍了如何在Swift 4中访问全局数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难通过parseJson()函数更改我的全局数组monthlyAvgArray.当我运行调试器时,它表明它在某一点具有值,但是当我尝试在函数外部访问它时,它说它为空.我尝试过从函数中返回一个数组并将其设置为与全局数组相等,但这也不起作用.

I'm having a hard Time changing an my global array monthlyAvgArray through my parseJson() function. When I run the debugger, it shows that it has values at one point, but then when I try to access it outside the function, it says its empty. I have tried returning an array from my function and setting it equal to the global array, but that didn't work either.

这是我的全局数组:

//declaring the dynamic arrays that will hold the information that is parsed
//will be using the same two arrays for each plant
var dateArray = [String]()
var monthlyAvgArray = [Double]()

这是我的parseJson(),它接受URL的字符串.

And here is my parseJson() that takes in a String for the URL.

func parseJson(URL_String:String) {
    let url = URL(string: EBR_String)
    //var monthlyDoubleArray = [Double]()
    // Load the URL
    URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
        // If there are any errors don't try to parse it, show the error
        guard let data = data, error == nil else { print(error!); return }

        let decoder = JSONDecoder()
        do{
            let plantData = try decoder.decode([Plant].self, from: data)
            //get each plant dictionary in plantData array of plant dictionaries
            for var eachPlant in plantData {
                // check to see if any values of the 30 day avg are null
                if (eachPlant.monthlyAVG == nil)
                {
                    //set null values to 
                    eachPlant.monthlyAVG = "0"
                }
                //print(eachPlant.date)
               // print(eachPlant.monthlyAVG!)
                let monthlyDouble = NSString(string: eachPlant.monthlyAVG!).doubleValue
                let monthlyDouble2 = Double(round(100*monthlyDouble)/100)
               // monthlyDoubleArray.append(monthlyDouble2)
                self.monthlyAvgArray.append(monthlyDouble2)
                //self.dateArray.append(eachPlant.date)
                //print(monthlyDouble)
            }
            // at this point my array has values
            print(self.monthlyAvgArray)
            //print(self.dateArray)
        }
        catch let err{print(err)}
    }).resume()
    //at this point my array does not have values
    print(monthlyAvgArray)
}

然后我在viewDidLoad()中调用我的parseJson()函数:

And I'm calling my parseJson() function in the viewDidLoad() here:

override func viewDidLoad() {
    super.viewDidLoad()
    //Initiatte calling the items download
   // parseJson(URL_String: EBR_String)
    parseJson(URL_String: EBR_String)
    SetGraph ()

}

我确实早些时候将全局数组设置为私有,因为我不希望它们在其他View Controller中被访问,但是我确实希望能够在此特定View Controller中的任何位置进行访问.我只是想知道为什么我一次要在数组中获取值,然后又什么也没有.谁能帮我吗?谢谢.

I did have the global arrays set to private earlier because I don't want them to be access in other View Controllers, but I do want to be be able to be access anywhere in this specific View Controller. I'm just wondering why I'm getting values in the array at one point, and then nothing afterwards. Can anyone help me out? Thanks.

推荐答案

最后一个print语句没有任何值的原因是因为仅在请求完成后才将数组追加到该值.由于dataTask是异步运行的,因此您将首先打印最后一个打印语句,然后再在完成处理程序内部打印该语句.因此,希望在命中完成块之前打印一个空数组,并期望在完成块数组中包含数据.

Looks like the reason that the last print statement doesn't have any values is because the array is only appended to after the request completes. As the dataTask is run asynchronously you would be printing the last print statement first and then printing the one inside of the completion handler after. So the printing of an empty array before the completion block is hit and having data within the completion blocks array would be expected.

这篇关于如何在Swift 4中访问全局数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆