ios - 如何根据 Swift 4 中 UISlider 的值使用 JSON 中的数据更改 UITextfield 文本?

标签 ios json swift uislider

现在我对 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/

相关文章:

ios - 在 Swift 中,如何在从 App Extension 目标运行时忽略部分代码?

ios - 使用 ReactiveCocoa 从对象数组链接异步操作

ios - Ipad 上网络浏览器中的 3D

ios - NSManagedObjectContext中的NSOperation解除分配崩溃

java - Ajax 请求 Spring REST api 415 错误

java - 使用 JSON 文件中的 ListView 的 Android 搜索功能(困难)

iOS swift JSON 到类对象

ios - UIButton 移动到自定义单元格中的 Storyboard

ios - SideMenu by jonkykong - 从右侧滑入

swift - 在 Swift switch 语句中使用 `let` 编译错误