在Swift中使用switch语句嵌套枚举的最佳方式? [英] Best way to nest enums for access by switch statements in Swift?

查看:396
本文介绍了在Swift中使用switch语句嵌套枚举的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 枚举位置{
枚举Top {
case left
case right
case Center
}
enum Bottom {
case left
case right
case Center
}
枚举left {
case顶部
case Bottom
案例中心
}
枚举右{
case顶部
case底部
案例中心
}
枚举中心{
案例中心
}
}

如果我尝试运行一个开关语句,没有一个枚举显示为可能的情况,如果我尝试列出它们,我会收到一个错误:

  func switchOverEnum(enumCase:Location){
switch enumCase {
case .Top:
print(嘿,这没有导致错误,没有它做)
}
}

错误我s:枚举大小写顶在位置类型中找不到。



现在有一个这个问题的版本此处,根据最有帮助的答案应该是这样做:

 枚举位置{
枚举TopLocations {
case左
案例右
案例中心
}
枚举BottomLocations {
案例左
案件右
案件中心
}
enum LeftLocations {
case顶部
案例底部
案例中心
}
枚举RightLocations {
案例顶部
案例底部
案例中心
}
枚举CenterLocations {
案例顶部
案例底部
案例左
案件右
案件中心
}
case顶部(TopLocations)
case Bottom(B ottomLocations)
case左(LeftLocations)
case右(RightLocations)
案例中心(CenterLocations)
}

哪些完全可以工作,但似乎有点笨重,或者不巧,或者不是Swift。这是真的最好的方法吗?

解决方案

我认为这将更简洁地表达与两个枚举和一个元组。在操场上尝试:

 枚举Horizo​​ntalPosition {
案例左
案例右
案中心
}

枚举VerticalPosition {
案顶
案底底
案中心
}

typealias位置=(horizo​​ntal:Horizo​​ntalPosition,vertical:VerticalPosition)

let aLocation = Location(horizo​​ntal:.Left,vertical:.Bottom)

切换aLocation {

case(.Left,.Bottom):print(left bottom)
case(.Center,.Center):print(center center)
default:print( )
}


I have a nested enum like so, for describing basic relative positioning:

  enum Location {
    enum Top {
      case Left
      case Right
      case Center
    }
    enum Bottom {
      case Left
      case Right
      case Center
    }
    enum Left {
      case Top
      case Bottom
      case Center
    }
    enum Right {
      case Top
      case Bottom
      case Center
    }
    enum Center {
      case Center
    }
  }

If I try to run a switch statement with it, none of the enums show up as possible cases, and if I try to list them I get an error:

func switchOverEnum(enumCase: Location) {
  switch enumCase {
  case .Top:
    print("hey this didn't cause an error whoops no it did")
  }
}

The error is: Enum case 'Top' not found in type 'Location'.

Now there's a version of this question here, and according to the most helpful answer, it should be done like this:

   enum Location {
    enum TopLocations {
      case Left
      case Right
      case Center
    }
    enum BottomLocations {
      case Left
      case Right
      case Center
    }
    enum LeftLocations {
      case Top
      case Bottom
      case Center
    }
    enum RightLocations {
      case Top
      case Bottom
      case Center
    }
    enum CenterLocations {
      case Top
      case Bottom
      case Left
      case Right
      case Center
    }
    case Top(TopLocations)
    case Bottom(BottomLocations)
    case Left(LeftLocations)
    case Right(RightLocations)
    case Center(CenterLocations)
  }

Which totally works, but seems a bit clunky, or inelegant, or un-Swift-like. Is this really the best way?

解决方案

I think this would be much more concisely expressed with two enums and a tuple. Try this in a playground:

enum HorizontalPosition {
    case Left
    case Right
    case Center
}

enum VerticalPosition {
    case Top
    case Bottom
    case Center
}

typealias Location = (horizontal: HorizontalPosition, vertical: VerticalPosition)

let aLocation = Location(horizontal: .Left, vertical: .Bottom)

switch aLocation {

case (.Left, .Bottom): print ("left bottom")
case (.Center, .Center): print ("center center")
default: print ("everything else")
}

这篇关于在Swift中使用switch语句嵌套枚举的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆