Swift switch statement for matching substrings of a String

前端 未结 7 1562
时光说笑
时光说笑 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:45

    Swift 5 Solution

    func weatherImage(for identifier: String) -> UIImage? {
        switch identifier {
        case _ where identifier.contains("Clear"),
             _ where identifier.contains("rain"):
            return self.soleadoImage
        case _ where identifier.contains("broken clouds"):
            return self.nubladoImage
        default: return nil
        }
    }
    

提交回复
热议问题