Swift将数组传递给函数返回

时间:2017-03-23 00:33:05

标签: ios swift firebase swift3 firebase-realtime-database

我需要这个数组

self.values.append(value)  

为了使用^上面数组的值附加另一个数组。我需要将一个数组传递给另一个带有附加值的函数。

 func updateChartValues() -> (LineChartDataSet) {

           self.recieveChartValues()

            var entries: [ChartDataEntry] = Array()

            for i in 0 ..< values.count {

                entries.append(ChartDataEntry(x: Double(i), y: Double(values[i]), data: UIImage(named: "icon", in: Bundle(for: self.classForCoder), compatibleWith: nil)))

如何获取从recieveChartValues()追溯到updateChartValues的这些值?主要的混淆是因为它们是从Firebase附加的。

func recieveChartValues() {

        //Firebase Initialization
        var ref: FIRDatabaseReference!
        ref = FIRDatabase.database().reference()

        ref.child("general_room_index").observeSingleEvent(of: .value, with: {(snap) in
            print("error3")



            if let snaps = snap.value as? [Any]{
                for val in snaps {
                    if let value = val as? Int{
                        self.values.append(value)
                        //print(self.values)
                    }
                }
            }

        })

    }//retrive values func



func updateChartValues() -> (LineChartDataSet) {

       self.recieveChartValues()

        var entries: [ChartDataEntry] = Array()

        for i in 0 ..< values.count {

            entries.append(ChartDataEntry(x: Double(i), y: Double(values[i]), data: UIImage(named: "icon", in: Bundle(for: self.classForCoder), compatibleWith: nil)))

            print("Enetrie ", entries)



        }



        self.dataSet = LineChartDataSet(values: entries, label: "Bullish vs. Bearish")
        self.dataSet.mode = LineChartDataSet.Mode.cubicBezier


        return dataSet

    }

1 个答案:

答案 0 :(得分:1)

就像Paulw11所说,关键是异步性质。如果你这样做

recieveChartValues()
updateChartValues()

recieveChartValues()运行(并立即返回),然后运行updateChartValues()(使用self.values而不附加新值),然后运行附加新值的完成闭包。

在完成闭包中直接调用updateChartValues(),如下所示:

ref.child("general_room_index").observeSingleEvent(of: .value, with: {(snap) in
    ...
    if let snaps = snap.value as? [Any] {
        for val in snaps {
            if let value = val as? Int{
                self.values.append(value)
                updateChartValues()
            }
        }
    }
})

或者向recieveChartValues()添加一个闭包参数,并在获得新值时调用它:

func recieveChartValues(_ completion: @escaping () -> Void) {
    ...
    if let snaps = snap.value as? [Any] {
        for val in snaps {
            if let value = val as? Int{
                self.values.append(value)
                completion()
            }
        }
    }
}

并按照以下方式致电recieveChartValues

recieveChartValues { self.updateChartValues() }

您还可以向完成闭包添加一个参数,以便您可以将收到的值传递给它,如果您对此感兴趣:

func recieveChartValues(_ completion: @escaping (Int) -> Void) {
    ...
            if let value = val as? Int{
                self.values.append(value)
                completion(value)
            }
    ...
}

然后将使用新值调用您的闭包:

recieveChartValues { newValue in
    print("Received \(newValue)")
    self.updateChartValues()
}
相关问题