-
-
Save davidandreoletti/1827c0f99b053e53e45d42690cf79442 to your computer and use it in GitHub Desktop.
Generic Delegate Pattern in Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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