Ad

Dictionary Get Nil

- 1 answer

what is wrong with xcode? Get nil for key 9.0

  @IBAction func sliderAction(_ sender: UISlider) {
        speed = round(sender.value / step) * step
        sender.value = speed
        speedLabel.text = modeDict[sender.value]
        print(sender.value)
        print(speedLabel.text)
    }

    let modeDict : [Float : String] = [ 0.8 : "Pls delete game, you noob", 0.9 : "Pff, girl", 1.0 : "Optimal", 1.1 : "Very Hard", 1.2 : "Almost Unreal"]
    let step : Float = 0.1
    var speed : Float = 0.0

    //

Print so, why 9.0 -> nil ?

0.8 Optional("Pls delete game, you noob")

0.9 nil

1.0 Optional("Optimal")

1.1 Optional("Very Hard")

1.2 Optional("Almost Unreal")

Ad

Answer

It's better to shift keys by 10

let modeDict : [Float : String] = [ 8 : "Pls delete game, you noob", 9 : "Pff, girl", 10 : "Optimal", 11 : "Very Hard", 12 : "Almost Unreal"]

and use

speedLabel.text = modeDict[sender.value*10]
Ad
source: stackoverflow.com
Ad