jeudi 24 mai 2018

Combine an IF LET with an OR in Swift

Is there a graceful way to combine two if let statements by an or operator. For instance, I need to check for the strings "pass", "true", or the integer 1. The following function, does just that...

func test(content: Any) -> String {
    if let stringValue = (content as? String)?.lowercased(),
        ["pass", "true"].contains(stringValue) {
        return "You Passed"
    }
    if let numValue = (content as? Int),
        1 == numValue {
        return "YOU PASSED"
    }
    return "You Failed"
}

test(content: "Pass") //"You Passed"
test(content: 1) //"YOU PASSED"

What is the most simple way to combine these two if let statements to handle the data being passed in?

Aucun commentaire:

Enregistrer un commentaire