Skip to content

Instantly share code, notes, and snippets.

@davidandreoletti
Forked from smokyonion/GenericDelegate.swift
Created September 11, 2017 22:24
Show Gist options
  • Save davidandreoletti/1827c0f99b053e53e45d42690cf79442 to your computer and use it in GitHub Desktop.
Save davidandreoletti/1827c0f99b053e53e45d42690cf79442 to your computer and use it in GitHub Desktop.

Revisions

  1. Vincent Wayne created this gist Aug 26, 2016.
    43 changes: 43 additions & 0 deletions GenericDelegate.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@

    public protocol DataSource: class {
    associatedtype AbstractType
    func source() -> AbstractType
    }

    class ViewController<T: DataSource>: UIViewController {

    weak var dataSouce: T?
    typealias AbstractType = T.AbstractType

    override func viewDidLoad() {
    super.viewDidLoad()

    let source: AbstractType? = dataSouce?.source()
    print("Source: ", source)
    }
    }

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, DataSource {

    var vc: ViewController<AppDelegate>?
    var window: UIWindow?

    typealias AbstractType = Date

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    vc = ViewController<AppDelegate>()
    vc?.dataSouce = self
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = vc
    window?.makeKeyAndVisible()
    return true
    }

    // MARK: - DataSource
    func source() -> AbstractType {
    return Date()
    }
    }