变量的值在observeSingleEvent中保持不变 [英] Value of variable not changing in observeSingleEvent

查看:71
本文介绍了变量的值在observeSingleEvent中保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

变量num更改为我在observeSingleEvent内部想要的值,但是在它变回空值之后.我如何获取num的值来更改?

The variable num changes to what I want inside the observeSingleEvent, but just after it changes back to an empty value. How do i get the value of num to change??

   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
     }

推荐答案

与Firebase传回的值相关的任何工作都必须在完成处理程序中完成(方法调用的with部分) .要使用/操纵从Firebase获得的num值,您需要在完成处理程序中使用它.

Any work that you need to do with values that are coming back from Firebase must be done within the completion handler (the with part of the method call). To use/manipulate the num value you get from Firebase, you need to use it within your completion handler.

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

        if self.num == 5 {
            print("number is 5")
        else {
            print("number is not 5")
        }
    }
})

这篇关于变量的值在observeSingleEvent中保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆