2016-02-28 79 views
0

我做了一些谷歌搜索和示例使用「,」使用多個聲明,但它不適用於我。我也嘗試過& &。Swift:如何在條件綁定中使用多個'where'?

if let movesDict = pokemonInfoDict["moves"] as? [Dictionary<String,AnyObject>] where movesDict.count > 0, movesDict["learn_type"] == "level up"{ 
} 

if let movesDict = pokemonInfoDict["moves"] as? [Dictionary<String,AnyObject>] where movesDict.count > 0 && movesDict["learn_type"] == "level up"{ 
} 

任何幫助將不勝感激謝謝。

+0

'&&'已經爲我工作了。還有什麼是錯的嗎? – tktsubota

+0

@TroyT我使用如果'讓movesDict = pokemonInfoDict [「移動」]爲? [Dictionary ] where movesDict.count> 0 && movesDict [「learn_type」] ==「level up」{}'但他們給了我這個錯誤**不能下標'[Dictionary ]'的索引類型'String'** –

回答

3

你想&& - 你必須有一些其他的問題,你的代碼,因爲這樣工作的:

let foo: Int? = 10 

if let bar = foo where bar > 8 && bar % 2 == 0 { 
    print("It works") 
} 
+0

我用'let movesDict = pokemonInfoDict [「moves」]爲? [Dictionary ] where movesDict.count> 0 && movesDict [「learn_type」] ==「level up」{}'但他們給了我這個錯誤**不能下標'[Dictionary ]',其索引類型爲'String'** –

+0

看起來你有一個字典數組,所以你可能想從第一個字符串或者其他東西獲得'learn_type'。 – javanut13

+0

我想爲整個數組使用movesDict [「learn_type」] ==「level up」,我該如何做,而不需要再遍歷整個數組。 –

1

你試過這樣:

if let movesDict = pokemonInfoDict["moves"] as? [Dictionary<String,AnyObject>] 
    where movesDict.count > 0 
     && movesDict["learn_type"] == "level up" 
{ 
    // ... 
} 

問題是movesDict是字典的一個數組,並且當您說movesDict["learn_type"]時,您試圖使用字符串"learn_type"作爲該數組的下標,但數組下標必須是Int

+0

我明白了,所以它應該像movesDict [0] [「learn_type」] ==「level up」,但我需要它是整個數組,不只是索引0.我如何在沒有循環整個字典呢? –

相關問題