符合单一协议的 Swift Enum 关联值
如何获取当前枚举案例的关联值作为可刷新而不使用详尽的开关? 在情况下,我只需要将其作为协议检索,该协议用于每种情况相关类型。
class Type1: Refreshable {}
class Type2: Refreshable {}
class Type3: Refreshable {}
protocol Refreshable {
func refresh()
}
enum ContentType {
case content1(Type1 & Refreshable)
case content2(Type2 & Refreshable)
case content3(Type3 & Refreshable)
func refreshMe() {
//self.anyValue.refresh() //Want simple solution to get to the refresh() method not knowing actual current state
}
}
How can I get associated value of current enum's case as Refreshable not using exhaustive switch?
In condition I only need it retrieved as protocol, which is used for each case associated type.
class Type1: Refreshable {}
class Type2: Refreshable {}
class Type3: Refreshable {}
protocol Refreshable {
func refresh()
}
enum ContentType {
case content1(Type1 & Refreshable)
case content2(Type2 & Refreshable)
case content3(Type3 & Refreshable)
func refreshMe() {
//self.anyValue.refresh() //Want simple solution to get to the refresh() method not knowing actual current state
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到了解决方案,以防有人也需要这个。
I've found the solution in case anyone will need this too.