How does let x where x.hasSuffix(“pepper”) work

后端 未结 2 1878
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 16:50

In the code block below, I am having trouble understanding let x where x.hasSuffix(\"pepper\").

let vegetable = \"red pepper\"

switch vegetable {
          


        
2条回答
  •  不思量自难忘°
    2021-02-14 17:24

    vegetable is an implicit String. It's the same as you would write:

    var vegetable: String = "red pepper"
    

    hasSuffix is declared as func hasSuffix(suffix: String) -> Bool an therefore returns a Bool. The where keyword specifies additional requirements, and can only be used in switch statements.
    Because all of this is suffused, the vegetable variable is assigned to x (let x).

    You can read more about the where and switch here.

提交回复
热议问题