Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@smokyonion
Created August 26, 2016 17:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smokyonion/3740054780628a162d9040323f691036 to your computer and use it in GitHub Desktop.
Save smokyonion/3740054780628a162d9040323f691036 to your computer and use it in GitHub Desktop.
Generic Delegate Pattern in Swift 3
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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment