Swift 是否可以保证枚举具有特定的情况?
我有一个协议:Swift 是否可以保证枚举具有特定的情况?,swift,enums,Swift,Enums,我有一个协议: protocol ReduxTransition {} 我有一个枚举: enum ProfileTransition: ReduxTransition { case authorization ... case showNotification(NotificationType) } enum RatingTransition: ReduxTransition { case pop ... case showNotification(No
protocol ReduxTransition {}
我有一个枚举:
enum ProfileTransition: ReduxTransition {
case authorization
...
case showNotification(NotificationType)
}
enum RatingTransition: ReduxTransition {
case pop
...
case showNotification(NotificationType)
}
我想这样做,以避免使用类似的实现来显示通知
func processError(performTransition: @escaping (ReduxTransition) -> ()) {
var notification: NotificationType!
performTransition(.showNotification(notification))
}
如果有人感兴趣,我应用了以下解决方案:
protocol NotificationPresentable {
static func getNotificationTransition(of type: NotificationType) -> Self
}
extension ProfileTransition: NotificationPresentable {
static func getNotificationTransition(of type: NotificationType) -> ProfileTransition {
return .showNotification(type)
}
}
extension RatingTransition: NotificationPresentable {
static func getNotificationTransition(of type: NotificationType) -> RatingTransition {
return .showNotification(type)
}
}
func processError<Transition: NotificationPresentable>(performTransition: @escaping (Transition) -> ()) {
let notification: NotificationType!
...
performTransition(Transition.getNotificationTransition(of: notification))
}
协议通知可呈现{
静态函数getNotificationTransition(类型:NotificationType)->Self
}
扩展配置文件转换:NotificationPresentable{
静态函数getNotificationTransition(类型:NotificationType)->ProfileTransition{
return.showNotification(类型)
}
}
扩展分级转换:NotificationPresentable{
静态函数getNotificationTransition(类型:NotificationType)->RatingTransition{
return.showNotification(类型)
}
}
func processError(执行转换:@转义(转换)->()){
让通知:NotificationType!
...
performTransition(Transition.getNotificationTransition(of:notification))
}
也许有人知道如何使这个解决方案变得更好?你是什么意思?如果只允许一种类型,那么将该类型全部删除,只需传入通知本身而不带num。我试图扩展我的问题,但仍然不知道您实际在问什么:
performTransition(通知)
和func processError(performTransition:@escaping(NotificationType)->(){
有效。在我的redux实现中,如果不使用enum,我无法启动转换。来自特定屏幕的所有转换都应封装在enum中。它们只能通过使用blockperformTransition:@escaping([ModuleName]Transition)->Void启动