从下面的代码:铸造和检查值多种类型
import Foundation
func checkStatus(statusObj: AnyObject) -> String {
if let status = statusObj as? String where status.lowercaseString == "ok" {
return "success"
} else if let status = statusObj as? Int where status >= 200 && status < 300 {
return "success"
} else {
return "failed"
}
}
print(checkStatus("ok"))
print(checkStatus(200))
print(checkStatus("error"))
print(checkStatus(500))
有没有办法将两个成功的条件组合成一个单独的语句?
这应该是你这样做的方式。合并可能会引入歧义。 –