赋值给它后,Swift变量仍为0?

LuK*_*eth 1 ios firebase swift

在我的viewDidLoad()中我打印出一个函数的结果

override func viewDidLoad() {
    super.viewDidLoad()
    print("top count = \(getCurrentOrderNum())")
}
Run Code Online (Sandbox Code Playgroud)

该函数计算如此值

func getCurrentOrderNum() -> Int{
    var orderNum = 0
    ref = Firebase(url: "urlhiddenforprivacy")
    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
        let count = snapshot.childrenCount
            orderNum = Int(count)
    })
    return orderNum
}
Run Code Online (Sandbox Code Playgroud)

但它还打印0?我试图将var orderNum:Int = Int()放在我的代码顶部,而不是放在我的getCurrentOrderNum函数中,但是这不起作用.我知道它在我的ref.observe函数中得到了正确的值,因为当我运行它时...它打印出正确的值

    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
        let count = snapshot.childrenCount
            orderNum = Int(count)
            print(orderNum) //*****THIS PRINTS THE RIGHT VALUE****
    })
    return orderNum
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*all 6

您正在异步块实际运行之前orderNum从该方法返回getCurrentOrderNum().所以在返回时,orderNum仍然是0你设置的初始值.该块稍后完成.

您最好的选择可能是将方法更改为:

func getCurrentOrderNum(callback:Int->()) {
    var orderNum = 0
    ref = Firebase(url: "urlhiddenforprivacy")
    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
        let count = snapshot.childrenCount
        orderNum = Int(count)
        callback(orderNum)
    })
}
Run Code Online (Sandbox Code Playgroud)

然后你会这样称呼它:

override func viewDidLoad() {
    super.viewDidLoad()
    getCurrentOrderNum { orderNum in print(orderNum) }
}
Run Code Online (Sandbox Code Playgroud)

这会将getCurrentOrderNum()方法更改为在完成检索正确值后回调到闭包.

更新:基于下面的评论,目标是做这样的事情:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int)->Int { 
    return getCurrentOrderNum() 
}
Run Code Online (Sandbox Code Playgroud)

这是一种异步方法:

class YourViewController : UIViewController, UITableViewDataSource {

    private var orderNumber:Int = 0
    private IBOutlet var tableView:UITableView!

    func getCurrentOrderNum(callback:Int->()) {
        ref = Firebase(url: "urlhiddenforprivacy")
        ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
            let count = snapshot.childrenCount
            callback(Int(count))
        })
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        getCurrentOrderNum {
            orderNum in
            //This code runs after Firebase returns the value over the network
            self.orderNumber = orderNum  // Set our orderNumber to what came back from the request for current order number
            self.tableView.reloadData()  // Now reload the tableView so it updates with the correct number of rows
        }
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    
        return self.orderNumber // When the view first loads, this will be 0 and the table will show nothing.  After the request to Firebase returns the value, this will be set to the right number, the table view will be reloaded, and it will call this method again to get the updated number of rows to display.
    }
}
Run Code Online (Sandbox Code Playgroud)