Skip to content

Instantly share code, notes, and snippets.

@jeamesbone
jeamesbone / SceneExample.swift
Last active October 19, 2016 04:53
Storyboard Dependency Injection - Example
// 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
@jeamesbone
jeamesbone / UIStoryboard+Scenes.swift
Created October 19, 2016 03:55
Storyboard Dependency Injection - UIStoryboard+Scenes.swift
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
}
}
@jeamesbone
jeamesbone / Scene+Defaults.swift
Created October 19, 2016 03:54
Storyboard Dependency Injection - Scene+Defaults.swift
extension Scene {
var identifier: String {
return String(describing: ViewController.self)
}
}
@jeamesbone
jeamesbone / Scene.swift
Last active October 19, 2016 03:53
Storyboard Dependency Injection - Scene.swift
protocol Scene {
associatedtype ViewController
var identifier: String { get }
func configureViewController(_ viewController: ViewController)
}
@jeamesbone
jeamesbone / composition.swift
Last active March 15, 2016 01:14
Swift Struct Composition
// 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
@jeamesbone
jeamesbone / UITableView+Animate.swift
Last active August 29, 2015 14:16
UITableView+Animate
// Copyright (c) 2015 Jeames Bone. All rights reserved.
import UIKit
extension UITableView {
func animate(animations: () -> Void) {
beginUpdates()
animations()
endUpdates()
}