ios – 如果让OR条件允许的话

前端之家收集整理的这篇文章主要介绍了ios – 如果让OR条件允许的话前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以使用 Swift的“OR”条件?

类似的东西(对于Dictionary< String,AnyObject>字典):

  1. if let value = dictionary["test"] as? String or as? Int {
  2. println("WIN!")
  3. }

解决方法

这没有任何意义,当你只有一个if语句时,你怎么能告诉值是Int还是String?但是你可以这样做:
  1. let dictionary : [String : Any] = ["test" : "hi","hello" : 4]
  2.  
  3.  
  4. if let value = dictionary["test"] where value is Int || value is String {
  5. print(value)
  6. }

(在Swift 2.0中测试过)

如果您需要根据类型执行不同的操作,也可以执行此操作:

  1. if let value = dictionary["test"] {
  2. if let value = value as? Int {
  3. print("Integer!")
  4. } else if let value = value as? String {
  5. print("String!")
  6. } else {
  7. print("Something else")
  8. }
  9. }

猜你在找的iOS相关文章