2014-12-03 59 views
0

我的問題是:我有一個枚舉類型爲鍵和名爲元組作爲值的字典。元組作爲字典值

private let positionDictionary:[Position:(shortG: String, longG: String, shortE: String, longE: String)] = 
[ .Goalkeeper:("TW","Torwart","GK","Goalkeeper"), 
    .CentralDefender:("IV","Innenverteidiger","CD","Central Defender"), 
    .LeftBack:("LV","Linker Verteidiger","LB","Left Back"), 
    .LeftWingBack:("LAV","Linker Außenverteidiger","LWB","Left Wing Back"), 
    .RightBack:("RV","Rechter Verteidiger","RB","Right Back"), 
    .RightWingBack:("RAV","Rechter Außenverteidiger","RWB","Right Wing Back")] 

這就是我想要做的這個類的一部分:

self.positionDictionary[.Goalkeeper].shortE 

但是,編譯器告訴我,他「找不到成員‘shortE’」。我錯過了什麼?我真的很感激任何幫助:]

回答

2

字典鍵下標返回可選值。你需要解開,或使用可選的招標:

if let goalkeepersShortE = self.positionDictionary[.Goalkeeper]?.shortE { 
    // ... 
} 
+0

或'self.positionDictionary [.Goalkeeper]!shortE' – Mateusz 2014-12-03 14:07:39

+0

不要用以前的評論。如果字典中沒有包含key的值,守門者會在運行時崩潰。總是檢查你的選擇權。 – kap 2014-12-03 14:25:33