Swift switch statement for matching substrings of a String

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

    I'd recommend using a dictionary instead, as a mapping between the substring you're searching for and the corresponding image:

    func image(for weatherString: String) -> UIImage? {
        let imageMapping = [
            "Clear": self.soleadoImage,
            "rain": self.soleadoImage,
            "broken clouds": self.nubladoImage]
        return imageMapping.first { weatherString.contains($0.key) }?.value
    }
    

    A dictionary gives you flexibility, adding new mappings is easy to do.

    0 讨论(0)
  • 2021-01-01 16:45

    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
    }
    

    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.

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 2021-01-01 16:46

    If you do this a lot, you can implement a custom ~= operator that defines sub-string matching:

    import Foundation
    
    public struct SubstringMatchSource {
        private let wrapped: String
    
        public init(wrapping wrapped: String) {
            self.wrapped = wrapped
        }
    
        public func contains(_ substring: String) -> Bool {
            return self.wrapped.contains(substring)
        }
    
        public static func ~= (substring: String, source: SubstringMatchSource) -> Bool {
            return source.contains(substring)
        }
    }
    
    extension String {
        var substrings: SubstringMatchSource {
            return SubstringMatchSource(wrapping: self)
        }
    }
    
    switch "abcdefghi".substrings {
        case "def": // calls `"def" ~= "abcdefghi".substrings`
            print("Found substring: def") 
        case "some other potential substring":
            print("Found \"some other potential substring\"")
        default: print("No substring matches found")
    }
    
    0 讨论(0)
  • 2021-01-01 16:48

    This link also describes overloading operator ~= which is actually used by the switch statement for matching cases to allow you to match regular expressions.

    0 讨论(0)
提交回复
热议问题