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?
if let
could be added to the first as anelse if
case, as the two are mutually exclusive. – ForbiddenBool
and than have a second function translate that to aString
but having 3 values, even with distinct types to mean the same, seems like a design problem on its own. Try to avoidAny
. – Sterilize