Swift switch statement for matching substrings of a String

前端 未结 7 1561
时光说笑
时光说笑 2021-01-01 15:55

Im trying to ask for some values from a variable. The variable is going to have the description of the weather and i want to ask for specific words in order to show differen

7条回答
  •  一整个雨季
    2021-01-01 16:46

    You can do this with a switch statement using value binding and a where clause. But convert the string to lowercase first!

    var desc = "Going to be clear and bright tomorrow"
    
    switch desc.lowercaseString as NSString {
    case let x where x.rangeOfString("clear").length != 0:
        println("clear")
    case let x where x.rangeOfString("cloudy").length != 0:
        println("cloudy")
    default:
        println("no match")
    }
    
    // prints "clear"
    

提交回复
热议问题