我试图将a的布尔值传递UISwitch给另一个类使用NSUserDefaults.由于某种原因,在包含开关的类中,if应该将值设置为的语句NSUserDefaults无法读取开关声明.
@IBOutlet var shrimpSwitch: UISwitch!
@IBOutlet var nutSwitch: UISwitch!
@IBOutlet var dairySwitch: UISwitch!
let switchState = NSUserDefaults.standardUserDefaults()
if shrimpSwitch.switch.on{
switchState.setBool(true, forKey: "shrimpSwitch")
}
else{
switchState.setBool(false, forKey: "shrimpSwitch")
}
if nutSwitch.on{
switchState.setBool(true, forKey: "nutSwitch")
}
else{
switchState.setBool(false, forKey: "nutSwitch")
}
if dairySwitch.on{
switchState.setBool(true, forKey: "dairySwitch")
}
else{
switchState.setBool(false, forKey: "dairySwitch")
}
Run Code Online (Sandbox Code Playgroud)
在第一个If语句(shrimpSwitch.on)中,它将说明预期声明.我是否宣布开关都错了?任何帮助,将不胜感激.谢谢
我目前正在尝试为iOS制作应用程序,但我无法获得一些简单的代码.基本上我需要从数组列表中随机选择5个元素而不重复元素.我有一个草稿,但它只显示一个元素.
这是我的代码:
let array1 = ["salmon", "turkey", "salad", "curry", "sushi", "pizza"]
let randomIndex1 = Int(arc4random_uniform(UInt32(array1.count)))
print(array1[randomIndex1])
Run Code Online (Sandbox Code Playgroud)