使用 Swift 4 为 UISwitch 加载 isOn 或 isOff?
Loading isOn or isOff for a UISwitch using Swift 4?
我正在开发一个项目,我需要使用 Swift 将开关加载为 .isOn 或 .isOff 4。我可以在项目的主情节提要中通过将开关设置为打开或关闭,但我希望根据数据库中每个用户的布尔值打开或关闭开关。
//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 根据数据库中的 true/false 值加载 on/off 有什么想法吗?
使用以下代码更改开关状态,参见here
self.switchBool1Outlet.setOn(true,
animated: true)
我正在开发一个项目,我需要使用 Swift 将开关加载为 .isOn 或 .isOff 4。我可以在项目的主情节提要中通过将开关设置为打开或关闭,但我希望根据数据库中每个用户的布尔值打开或关闭开关。
//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 根据数据库中的 true/false 值加载 on/off 有什么想法吗?
使用以下代码更改开关状态,参见here
self.switchBool1Outlet.setOn(true,
animated: true)