Reputation: 7
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?
Upvotes: 1
Views: 74
Reputation: 535304
It would be better to use a dictionary that codes the numeric values as keys and the desired corresponding info (probably as a custom struct) as values. That way, it is a simple lookup directly from the slider value to the info.
Upvotes: 1