现在我对 UIText 字段进行了硬编码,并根据 UISlider 上的值进行更改。
@IBAction func sliderValueChanged(_ sender: UISlider) {
let currentValue = Int(sender.value)
levelLabel.text = "Food Level \(currentValue)"
if currentValue == 1 {
foodDescription.text = "Healthy Vegetables: (raw, steamed, roasted, frozen). "
我能够连接到 REST api 并解析 JSON 结果。
现在, slider 的值从 1 到 6。
JSON 的结构如下:
{
"Level1": "3",
"LevelCategory": "This includes all meats, seafood and poultry. We recommend organic sources here. Eggs (with and without yolk) are also in this section.",
"LevelID": "1275a833-b250-4de0-b29a-0b926e57bff0",
"LevelName": "Proteins"
}
使用 JSON 数据检查 slider 值并获取正确级别类别、名称等的最佳方法是什么?
现在,我使用 IF 作为 slider 值。有更好的方法吗?
最佳答案
最好使用将数值编码为键并将所需的相应信息(可能作为自定义结构)编码为值的字典。这样,它就是一个直接从 slider 值到信息的简单查找。
关于ios - 如何根据 Swift 4 中 UISlider 的值使用 JSON 中的数据更改 UITextfield 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431771/