相关疑难解决方法(0)

使用Swift的预期声明错误

我试图将a的布尔值传递UISwitch给另一个类使用NSUserDefaults.由于某种原因,在包含开关的类中,if应该将值设置为的语句NSUserDefaults无法读取开关声明.

ViewController.swift

@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)中,它将说明预期声明.我是否宣布开关都错了?任何帮助,将不胜感激.谢谢

xcode nsuserdefaults swift

16
推荐指数
1
解决办法
2万
查看次数

从数组列表中随机选择5个元素,而不重复元素

我目前正在尝试为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)

arrays random swift swift3

0
推荐指数
1
解决办法
1736
查看次数

标签 统计

swift ×2

arrays ×1

nsuserdefaults ×1

random ×1

swift3 ×1

xcode ×1