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
// First we need a scene for our ViewController | |
struct LandingScene: Scene { | |
// Non optional, safe references to our vc's dependencies | |
let client: APIClient | |
let account: Account | |
// Note the parameter type is the specific view controller we want to configure | |
// This takes advantage of the `associatedtype` we defined in our protocol | |
func configureViewController(_ viewController: LandingViewController) { | |
// Inject the dependencies |
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
extension UIStoryboard { | |
func instantiateViewController<S: Scene>(for scene: S) -> S.ViewController { | |
guard let viewController = instantiateViewController(withIdentifier: scene.identifier) as? S.ViewController | |
else { | |
fatalError("expected view controller with identifier '\(scene.identifier)' to be of type '\(String(describing: S.ViewController.self))'") | |
} | |
scene.configureViewController(viewController) | |
return viewController | |
} | |
} |
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
extension Scene { | |
var identifier: String { | |
return String(describing: ViewController.self) | |
} | |
} |
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
protocol Scene { | |
associatedtype ViewController | |
var identifier: String { get } | |
func configureViewController(_ viewController: ViewController) | |
} |
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
// Value types are awesome, but we lose the ability to inherit functionality if we use structs in swift. | |
// | |
// Below are some experiments where I've been playing with composition to solve this for a common use case | |
// where we want to have some common base model fields. | |
import Foundation | |
// Create a struct to hold our common fields | |
struct CommonFields { | |
let id: Int |
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
// Copyright (c) 2015 Jeames Bone. All rights reserved. | |
import UIKit | |
extension UITableView { | |
func animate(animations: () -> Void) { | |
beginUpdates() | |
animations() | |
endUpdates() | |
} |