如何访问在闭包中创建的数组?

时间:2019-04-05 19:18:58

标签: ios swift closures

我创建了scheduleDict1并将其插入scheduleArray1中。我只能像在代码中那样在闭包中声明闭包,才能让闭包“看到” scheduleArray1。但是,除了闭包之外,我无法访问scheduleArray1。

我尝试在MainViewController类而不是闭包中声明scheduleArray1,但在闭包内将看不到它!

import UIKit
import Firebase


class MainViewController: UITableViewController {

  //  var scheduleArray1 = [[String: String]]()
    var scheduleDict = [String: Any]()

    override func viewDidLoad() {
        super.viewDidLoad()
        retrieveSchedule()
        }

    func retrieveSchedule(){
        let ref = Database.database().reference(withPath: "squadrons/vt-9/2019-04-02/events/")
        ref.observe(.value) { (snapshot) in
            var scheduleArray1 = [[String: String]]()
            var count = 0
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() {
                let refToPost = Database.database().reference(withPath: "squadrons/vt-9/2019-04-02/events/" + "\(count)")
                refToPost.observe(.value, with: { (snapshot) in
            //      let data = snapshot.children
                    let scheduleDict1 = snapshot.value as! [String: String]
                    scheduleArray1.append(scheduleDict1)

                  //  self.print(scheduleArray1)
                })
                count += 1
            }
        }

    }   
}

0 个答案:

没有答案