我正在写一个快速的游戏,想写一个短期,中期和长期游戏的目标目标清单。这只是现金目标的线性列表。
enum GameLength : Int {
case ShortGame
case MediumGame
case LongGame
static let allValues = [
GameLength.ShortGame.rawValue,
GameLength.MediumGame.rawValue,
GameLength.LongGame.rawValue,
]
}
struct GameGoal {
// I think this should be a set as the game lengths cannot repeat
var cashGoals = [ [GameLength.ShortGame: 100] ,
[GameLength.MediumGame: 200] ,
[GameLength.LongGame: 300] ]
func target(gameLength:GameLength) {
var result = cashGoals[gameLength].first
print (result)
}
}
var gameLength:GameLength = .MediumGame
var gameGoal = GameGoal().target(gameLength)
print (gameGoal)
这里的问题是,我现在似乎无法访问给定目标的值。
理想情况下,我想用一个值映射gamelength枚举。
他们分开的原因是因为我以后需要对现金目标进行加权。
也许我把问题搞得太复杂了。
但无论如何;
问题>如何访问键为枚举的字典,并且只获取与值匹配的第一个枚举
最佳答案
实际上,您可以设置枚举的原始值,这样就不必处理该字典。
enum GameLength : Int {
case ShortGame = 100
case MediumGame = 200
case LongGame = 300
}
然后您可以通过
gameLength.rawValue