Skip to content

Instantly share code, notes, and snippets.

@ninjazoete
Created February 2, 2016 08:29
Show Gist options
  • Save ninjazoete/b5982d81fcf9855f211f to your computer and use it in GitHub Desktop.
Save ninjazoete/b5982d81fcf9855f211f to your computer and use it in GitHub Desktop.
Handle to generic protocol conforming type
protocol PresentationItem { }
protocol AnyPresenter {
func _present(item: Any) -> Void
}
protocol Presenter: AnyPresenter {
typealias PresentationType
func present(item: PresentationType) -> Void
}
extension Presenter {
func _present(state: Any) -> Void {
if let typed = state as? PresentationType {
present(typed)
}
}
}
struct UserPresentationItem : PresentationItem { }
class View : Presenter {
func present(item: UserPresentationItem) {
// Use item...
}
}
let presenter = View()
let anyPresenter: AnyPresenter = presenter
anyPresenter._present(UserPresentationItem())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment