Swift - 仅针对其中一种情况扩展枚举
Swift - extend an enum for only one of it's cases
所以我有一个枚举来控制视图控制器的实例以及所述视图控制器的创建。见下文:
enum RGOSessionsTableType {
case Active
case Scheduled
case Finished
func instantiateViewController() -> RGOSessionsTableViewController {
var id = ""
switch self {
case .Active: id = "ActiveSessionsTVC"
case .Scheduled: id = "ScheduledSessionsTVC"
case .Finished: id = "FinishedSessionsTVC"
}
return UIStoryboard(name: "MainView", bundle: nil).instantiateViewControllerWithIdentifier(id) as! RGOSessionsTableViewController
}
}
我想在存储实例化视图控制器的枚举上创建一个 属性,但是出于我不会解释的原因(主要是为了便于与现有代码集成),我希望使用特定的 class 类型分别为 Active、Scheduled 和 Finished。它们都继承自 RGOSessionsViewController
,但正如我提到的,我宁愿不制作这种较低级别类型的 属性。
我试过以下无法编译的方法,我以前从未在扩展中使用过 where 子句,所以很可能我的语法有误,或者根本不可能:
extension RGOSessionsTableType where self == .Active {
}
感谢任何和所有建议。
从评论中移出:
I think here enum is not a best idea. You should try to do this with protocol + 3 classes/structs implementing it. That will allow you both extend "only one case" and add more "cases" in it. Like here.
有关链接文章的快速信息:
Why I don’t love Swift Enums any more
At the last two Sydney Cocoaheads, any time I see a use of Enums in a talk, I make sure to ask “Why an enum and not a protocol?”. This blog post will hopefully explain the smell of enums.
Firstly, I have to say that enums are great if you never need to extend the number of cases. However, incorrect usages of enums break the O in SOLID Principles.
Open Close Principle
A Type should be open to extension, but closed for edits.
And I believe the Expression Problem best explains the violation.
The Expression Problem
Add new methods; add new cases. Pick one.
I will attempt to walk you through the Expression Problem with enums, then proceed to “solve” it with the Visitor Pattern.
所以我有一个枚举来控制视图控制器的实例以及所述视图控制器的创建。见下文:
enum RGOSessionsTableType {
case Active
case Scheduled
case Finished
func instantiateViewController() -> RGOSessionsTableViewController {
var id = ""
switch self {
case .Active: id = "ActiveSessionsTVC"
case .Scheduled: id = "ScheduledSessionsTVC"
case .Finished: id = "FinishedSessionsTVC"
}
return UIStoryboard(name: "MainView", bundle: nil).instantiateViewControllerWithIdentifier(id) as! RGOSessionsTableViewController
}
}
我想在存储实例化视图控制器的枚举上创建一个 属性,但是出于我不会解释的原因(主要是为了便于与现有代码集成),我希望使用特定的 class 类型分别为 Active、Scheduled 和 Finished。它们都继承自 RGOSessionsViewController
,但正如我提到的,我宁愿不制作这种较低级别类型的 属性。
我试过以下无法编译的方法,我以前从未在扩展中使用过 where 子句,所以很可能我的语法有误,或者根本不可能:
extension RGOSessionsTableType where self == .Active {
}
感谢任何和所有建议。
从评论中移出:
I think here enum is not a best idea. You should try to do this with protocol + 3 classes/structs implementing it. That will allow you both extend "only one case" and add more "cases" in it. Like here.
有关链接文章的快速信息:
Why I don’t love Swift Enums any more
At the last two Sydney Cocoaheads, any time I see a use of Enums in a talk, I make sure to ask “Why an enum and not a protocol?”. This blog post will hopefully explain the smell of enums.
Firstly, I have to say that enums are great if you never need to extend the number of cases. However, incorrect usages of enums break the O in SOLID Principles.
Open Close Principle
A Type should be open to extension, but closed for edits.
And I believe the Expression Problem best explains the violation.
The Expression Problem
Add new methods; add new cases. Pick one.
I will attempt to walk you through the Expression Problem with enums, then proceed to “solve” it with the Visitor Pattern.