【发布时间】:2018-02-05 13:33:34
【问题描述】:
我正在开发一个项目,我需要使用 Swift 4 将开关加载为 .isOn 或 .isOff。我可以在项目的主情节提要中通过将开关设置为打开或关闭来执行此操作,但我希望根据我数据库中每个用户的布尔值打开或关闭开关。
//Create outlet for UISwitch
@IBOutlet weak var switchBool1Outlet: UISwitch!
viewDidLoad() {
//pulling data from Google Firebase to get true or false value
dataRef.reference().child("USERS/\(uid!)").observe(.value, with: { snapshot in
if let dictionary = snapshot.value as? [String: Any]
{
//MARK - Local Variables
var bool1Value = dictionary["bool1"] as! Bool
//trying to set switches here, this is not working
if bool1Value == true && bool1Value != nil {
//following throws error: "Expression resolves to an unused l-value"
self.switchBool1Outlet.isOn
}
})
}
关于如何根据数据库中的真/假值让 UISwitch 加载/关闭有什么想法吗?
【问题讨论】:
-
你没有指定
mealPlan是什么。 -
@M.Wojcik 为了这个例子,打算改变那个 bool1Value。我的应用程序与我大学的用餐分享有关。欣赏评论!
-
谢谢,@Adrian!该链接非常有帮助,并帮助我回答了其他几个问题。我很感激!