Swift开关语句,用于匹配字符串的子字符串 [英] Swift switch statement for matching substrings of a String
问题描述
我正在尝试从变量中获取一些值. 该变量将具有天气的描述,我想询问特定的单词以显示不同的图像(例如,太阳,雨水或类似的东西) 事情是我有这样的代码:
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 different images (like a sun, rain or so) The thing is i have code like this:
if self.descriptionWeather.description.rangeOfString("Clear") != nil
{
self.imageWeather.image = self.soleadoImage
}
if self.descriptionWeather.description.rangeOfString("rain") != nil
{
self.imageWeather.image = self.soleadoImage
}
if self.descriptionWeather.description.rangeOfString("broken clouds") != nil
{
self.imageWeather.image = self.nubladoImage
}
因为当我尝试添加"OR"条件时,xcode给了我一些奇怪的错误.
Because when i tried to add an "OR" condition xcode gives me some weird errors.
有可能用这样的句子做一个夹心句子吗?还是有人知道如何在if子句中添加OR条件?
Is it possible to do a swich sentence with that? Or anyone knows how to do add an OR condition to the if clause?
推荐答案
Swift语言具有两种OR
运算符-按位运算符|
(单竖线)和逻辑运算符||
(双竖线)线).在这种情况下,您需要逻辑OR
:
Swift language has two kinds of OR
operators - the bitwise ones |
(single vertical line), and the logical ones ||
(double vertical line). In this situation you need a logical OR
:
if self.descriptionWeather.description.rangeOfString("Clear") != nil || self.descriptionWeather.description.rangeOfString("clear") != nil {
self.imageWeather.image = self.soleadoImage
}
与Objective-C不同,在Objective-C中,您可以放弃按位的OR
来换取稍有不同的运行时语义,而Swift则需要在上面的表达式中添加逻辑OR
.
Unlike Objective-C where you could get away with a bitwise OR
in exchange for getting a slightly different run-time semantic, Swift requires a logical OR
in the expression above.
这篇关于Swift开关语句,用于匹配字符串的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!