如果与OR条件一起使用 [英] if let with OR condition
本文介绍了如果与OR条件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过Swift的if let
使用或"条件?
Is it possible to use an "OR" condition using Swift's if let
?
类似的东西(对于Dictionary<String, AnyObject>
词典):
Something like (for a Dictionary<String, AnyObject>
dictionary):
if let value = dictionary["test"] as? String or as? Int {
println("WIN!")
}
推荐答案
这没有任何意义,当您只有一个if语句时,如何分辨value是Int还是String?但是,您可以执行以下操作:
This would make no sense, how would you be able to tell whether value is an Int or a String when you only have one if statement? You can however do something like this:
let dictionary : [String : Any] = ["test" : "hi", "hello" : 4]
if let value = dictionary["test"] where value is Int || value is String {
print(value)
}
(在Swift 2.0中测试)
(Tested in Swift 2.0)
如果您需要根据类型进行其他操作,也可以执行以下操作:
You can also do this if you need to do different things depending on the types:
if let value = dictionary["test"] {
if let value = value as? Int {
print("Integer!")
} else if let value = value as? String {
print("String!")
} else {
print("Something else")
}
}
这篇关于如果与OR条件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文