swift – 使用原始值进行枚举

前端之家收集整理的这篇文章主要介绍了swift – 使用原始值进行枚举前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么我不能用这样的原始值定义枚举?
  1. enum Edges : (Double,Double) {
  2. case TopLeft = (0.0,0.0)
  3. case TopRight = (1.0,0.0)
  4. case BottomLeft = (0.0,1.0)
  5. case BottomRight = (1.0,1.0)
  6. }
Because

Raw values can be strings,characters,or any of the integer or floating-point number types.

但是有一个替代解决方案:

  1. enum Edges {
  2. case TopLeft
  3. case TopRight
  4. case BottomLeft
  5. case BottomRight
  6.  
  7. func getTuple() -> (Double,Double) {
  8. switch self {
  9. case .TopLeft:
  10. return (0.0,0.0)
  11. case .TopRight:
  12. return (1.0,0.0)
  13. case .BottomLeft:
  14. return (0.0,1.0)
  15. case .BottomRight:
  16. return (1.0,1.0)
  17. }
  18. }
  19. }
  20.  
  21. let a = Edges.BottomLeft
  22. a.getTuple() // returning (0,1)

猜你在找的Swift相关文章