2017-10-03 3 views
0

私はプロトコルを持っている:列挙型に特定のケースがあることを保証できますか?

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)) 
} 
+1

どういう意味ですか? 1つの型だけが許可されている場合は、型をすべて削除し、numを指定しないで通知自体を渡します。 – luk2302

+0

私の質問を延長しようとしました –

+0

'performTransition(notification)'と 'func processError(performTransition:@escaping(NotificationType) - >()){'は実際に何をしているのかまだ分かりません。 – luk2302

答えて

0

た場合を示すために同様の実装を使用しないように、このようなものをやってみたいです誰もが興味を持って、私は次のソリューションを適用しました:

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)) 
} 

誰かがこのソリューションをさらに改善する方法を知っているのでしょうか?

関連する問題