簡體   English   中英

Swift:如何在條件綁定中使用多個“ where”?

[英]Swift: How to use more than one 'where' on conditional binding?

我做了一些谷歌搜索,示例使用“”來使用多個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"{
}

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

您需要&& -您的代碼必須存在其他一些問題,因為它可以起作用:

let foo: Int? = 10

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

您嘗試了這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM