变量的值在observeSingleEvent中保持不变

编程入门 行业动态 更新时间:2024-10-25 16:29:43
本文介绍了变量的值在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中保持不变

本文发布于:2023-07-15 10:30:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   observeSingleEvent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!