【问题标题】:Swift passing array to function with returnSwift将数组传递给函数并返回
【发布时间】:2017-03-23 00:33:04
【问题描述】:

我需要这个数组

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

    }

【问题讨论】:

  • 由于 Firebase 任务将异步完成,您需要将闭包传递给 recieveChartValues 并从 Firebase 完成闭包调用该闭包
  • 你能告诉我一个例子如何做到这一点
  • 我有点理解完成和关闭我只是不知道如何用这两个函数实现它们我需要一个例子来完全理解
  • 有人吗?谁能帮帮我?

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


【解决方案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()
}

【讨论】:

    猜你喜欢
    • 2020-08-19
    • 2018-09-23
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多