Advertisement
Guest User

Untitled

a guest
May 5th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4.  
  5. protocol MythicalCreatureDisplayable: class {
  6. associatedtype Conformer = Self
  7.  
  8. var displayer: Conformer { get }
  9. func displayMythicalCreature()
  10. }
  11.  
  12. final class KrakenView: UIView, MythicalCreatureDisplayable {
  13. func displayMythicalCreature() {
  14. print("draw a Kraken")
  15. }
  16. }
  17.  
  18. extension MythicalCreatureDisplayable where Self: UIView {
  19. var displayer: UIView { return self }
  20. }
  21.  
  22. private extension MythicalCreatureDisplayable {
  23. func getDisplayer() -> Conformer {
  24. return displayer
  25. }
  26. }
  27.  
  28. final class AnyMythicalTypeDisplayable<T>: MythicalCreatureDisplayable {
  29. var displayer: T { return _getDisplayer() }
  30.  
  31. private let _getDisplayer: () -> T
  32. private let _displayMythicalCreature: () -> Void
  33.  
  34. required init<U: MythicalCreatureDisplayable where U.Conformer == T>(_ displayable: U) {
  35. _getDisplayer = displayable.getDisplayer
  36. _displayMythicalCreature = displayable.displayMythicalCreature
  37. }
  38.  
  39. func displayMythicalCreature() {
  40. _displayMythicalCreature()
  41. }
  42. }
  43.  
  44. let krakenView = KrakenView()
  45. let regularView = UIView()
  46. var mythicalDisplayableView: AnyMythicalTypeDisplayable<UIView>
  47. mythicalDisplayableView = AnyMythicalTypeDisplayable(krakenView)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement