Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 是否可以保证枚举具有特定的情况?_Swift_Enums - Fatal编程技术网

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中。它们只能通过使用block
performTransition:@escaping([ModuleName]Transition)->Void启动