How to change the UITextfield text with data from JSON based on the value from UISlider in Swift 4?

Right now I have the UIText field hard coded and changing according to the value on the 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). "

I was able to connect to the REST api and get the JSON results parsed.

Now, the slider has values from 1 to 6.

The JSON is structured like this:

{
“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”
}

What’s the best way to check the slider value and get the correct Level Category, Name etc. using the JSON data?

Right now, I’m using IF for the slider value. Is there a better way to do this?

#ios #xcode #swift #json

3 Likes2.75 GEEK