变量值在observeSingleEvent中不变

时间:2017-03-28 13:57:36

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

变量num在observeSingleEvent中更改为我想要的内容,但是在它变回空值之后。我怎样才能得到num的值来改变?

   var num = Int()
   FIRDatabase.database().reference().child("menus/Day1").observeSingleEvent(of: .value, with: {(snap) in

    print(snap)

    if let snapDict = snap.value as? [String:AnyObject]{

        self.num = snapDict["date"] as! Int
        print(num)
        //returns the number I want

    }
    })

    print(num)
    //Returns an empty value

    if num == 5 {
        print("number is 5")
    else {
        print("number is not 5")
        //goes to this
     }

1 个答案:

答案 0 :(得分:1)

您需要对从Firebase返回的值所做的任何工作必须在完成处理程序(方法调用的OnServerAddPlayer部分)内完成。要使用/操作从Firebase获得的to_up = 0; // this will contain the number of 0.25 increases no_p1 = 0; // this will contain the times p1 needs to be spent ... no_pn = 0; starting_amount = 42.0; // specify your starting amount here WHILE (true) // to_up loop WHILE (true) // number of p1 WHILE (true) // number of p2 // ... repeat with as many different prices you have // now check for solution: IF(starting_amount - to_up * 0.25 - no_p1 * p1 - ... - no_pn * pn == 0) BEGIN print 'found solution: ups: %1, no p1: %2, ..., no pn: %n', no_p1, ..., no_pn EXIT(0); END no_p2++; END no_p1++; END to_up++; END 值,您需要在完成处理程序中使用它。

with