斯威夫特 - 类别枚举(或大意的东西)
我想要做这样的事情...斯威夫特 - 类别枚举(或大意的东西)
enum SpriteFrames:Int {
case N
case NE
case E
case SE
case S
case SW
case W
case NW
}
enum DroidSpriteFramesIdle:Int {
var image:String {
switch self {
case N:
return "droid_n"
case NE:
return "droid_ne"
case E:
return "droid_e"
case SE:
return "droid_se"
case S:
return "droid_s"
case SW:
return "droid_sw"
case W:
return "droid_w"
case NW:
return "droid_nw"
}
}
}
enum DroidSpriteFramesMove:Int {
var image:String {
switch self {
case N:
return "droid_move_n"
case NE:
return "droid_move_ne"
case E:
return "droid_move_e"
case SE:
return "droid_move_se"
case S:
return "droid_move_s"
case SW:
return "droid_move_sw"
case W:
return "droid_move_w"
case NW:
return "droid_move_nw"
}
}
}
...其中DroidSpriteFramesIdle
和DroidSpriteFramesMove
子SpriteFrames
,或符合它作为一个协议或有这样的结果。基本上,我希望有很多枚举都具有相同的方向列表(N,NE,E等),但对于这些常用方向有自己的唯一值列表。我不想使用类,因为这会导致在分配时创建实例。不过,对于其他想法,我对Swift很陌生。谢谢。
我相信你可以利用这篇文章,特别是枚举部分。它显示了一些与你的想法有关的东西。 http://www.objc.io/issue-16/power-of-swift.html
从上述网站引用
“他们是非常相似的。如果你改变的情况下,命名,这是不同的唯一事情是相关联的值,如果您还添加了一个值的零的情况下可选的,你最终得到的任一类型:
enum Either<A,B> { case Left(A) case Right(B) }
的任一类型的使用了很多,当你想表示两件事情之间的选择功能的编程举例来说,如果你有函数,返回无论是整数还是错误,都可以使用Ei疗法。如果您想要在字典中存储布尔值或字符串,可以使用Either作为键类型。
理论上抛开:有时枚举是所谓的总和类型,因为它们表示不同类型的总和。在任一情况下,它们表示A和B的总和。结构或元组称为产品类型,因为它们表示不同类型的产品。另请参阅:代数数据类型“
谢谢@Ashraf,我无法使您的建议适应我的代码,但它确实使我得到了一个令人满意的结论,我最终编写了一些函数接受枚举作为参数,这是掉踪点点从我最初的做法,但它蜱所有的箱子,例如具有很少的代码重复,仍然是避免不必要的实例化功能的方法最终代码在这里:。
enum Direction: Int {
case N
case NE
case E
case SE
case S
case SW
case W
case NW
}
enum Action: Int {
case Idle
case Move
}
func spriteFramesDroid(direction:Direction, action:Action) -> String {
switch action {
case .Idle:
switch direction {
case .N:
return "droid_n"
case .NE:
return "droid_ne"
case .E:
return "droid_e"
case .SE:
return "droid_se"
case .S:
return "droid_s"
case .SW:
return "droid_sw"
case .W:
return "droid_w"
case .NW:
return "droid_nw"
}
case .Move:
switch direction {
case .N:
return "droid_move_n"
case .NE:
return "droid_move_ne"
case .E:
return "droid_move_e"
case .SE:
return "droid_move_se"
case .S:
return "droid_move_s"
case .SW:
return "droid_move_sw"
case .W:
return "droid_move_w"
case .NW:
return "droid_move_nw"
}
}
}
您的解决方案比我想象的要好。做得好! :) – 2015-02-10 11:53:45
你可以为该枚举创建一个别名,它可以像子类一样行动
ex苹果的默认值:
public enum UITextFieldViewMode : Int {
case Never
case WhileEditing
case UnlessEditing
case Always
}
前别名:
public typealias AutoCompleteButtonViewMode = UITextFieldViewMode
你的函数:
func showButtonWithViewMode(buttonViewMode: AutoCompleteButtonViewMode) {
// Code here
}
用法:
showButtonWithViewMode(.WhileEditing)
也许分配枚举原始值会更具可读性。
enum Direction:String {
case N = "n"
case NE = "ne"
case E = "e"
case SE = "se"
case S = "s"
case SW = "sw"
case W = "w"
case NW = "nw"
}
enum Action: String {
case Idle = "droid"
case Move = "droid_move"
}
func spriteFramesDroid(direction:Direction, action:Action) -> String {
return "\(action.rawValue)_\(direction.rawValue)"
}
请看看http:// stackoverflow。com/help/how-to-answer:“**为链接提供上下文**鼓励链接到外部资源,但请在链接上添加上下文,以便您的同行用户了解它是什么以及它为什么在那里。引用重要链接中最相关的部分,以防目标站点无法访问或永久脱机。“ – 2015-02-10 09:55:31
该链接很有趣,但没有回答这个问题。我认为Swift根本无法做到要求。 – 2015-02-10 10:02:07