我之前曾问过类似的问题,但答案似乎不起作用 .
我有一个名为Settings的字典:
var settings = String:Any
这是通过阅读文本文件来填充的 - 一切正常
当我运行 println(settings)
时,它会返回填充的字典 .
[monsterRate: 1.0, monsterMinSpeed: 10.0, weaponPickupRate: 10.0, weaponPickupAmount: 50.0, goldPerMonster: 10.0, totalMonsters: 10.0, LevelNum: 1.0, monsterMaxSpeed: 15.0]
如果我运行 println(settings["monsterMinSpeed"])
它返回 Optional("10.0")
但是,当我尝试将变量设置为字典保存的值时,它不起作用:
monsterMinSpeed = (settings["monsterMinSpeed"] as? Double) ?? 0.0
monsterMaxSpeed = (settings["monsterMaxSpeed"] as? Double) ?? 0.0
monsterRate = (settings["monsterRate"] as? Double) ?? 0.0
weaponPickupAmount = (settings["weaponPickupAmount"] as? Double) ?? 0.0
weaponPickupKills = (settings["weaponPickupKills"] as? Double) ?? 0.0
goldPerMonster = (settings["goldPerMonster"] as? Double) ?? 0.0
即使字典中有值,它总是使用0.0,好像我提供的键不存在 - 返回nil,因此将变量设置为0.0
有人可以帮忙吗?
1 回答
那些是以字符串形式出现的,而不是数字 - 尝试类似于: