"as" in Swift

前端之家收集整理的这篇文章主要介绍了"as" in Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

逐条解释一下,熟悉 “as”的用法,是“as”,不是“as?”和 “as!”:

  1. for thing in things {
  2. switch thing {
  3. case 0 as Int:
  4. print("zero as an Int")
  5.  
  6. case 0 as Double:
  7. print("zero as a Double")
  8.  
  9. case let someInt as Int:
  10. print("an integer value of \(someInt)")
  11.  
  12. case let someDouble as Double where someDouble > 0:
  13. print("a positive double value of \(someDouble)")
  14.  
  15. case is Double:
  16. print("some other double value that I donot want to print")
  17.  
  18. case let someString as String:
  19. print("a string value of \"\(someString)\"")
  20.  
  21. case let (x,y) as (Double,Double):
  22. print("an (x,y) point at \(x),\(y)")
  23.  
  24. case let movie as Movie:
  25. print("a movie called \(movie.name),dir.\(movie.director)")
  26.  
  27. case let stringConverter as (String) -> String:
  28. print(stringConverter("Michael"))
  29.  
  30. default:
  31. print("something else")
  32.  
  33. }
  34. }
  1. (1) 0 as Int
  2.  
  3. (2) 0 as Double

猜你在找的Swift相关文章