Skip to content

Instantly share code, notes, and snippets.

@cjnevin
cjnevin / Identifiable.swift
Last active March 28, 2018 07:40
Protocol for identifying types
import UIKit
protocol Identifiable: class {
static var identifier: String { get }
}
extension Identifiable {
static var identifier: String {
return String(describing: self)
}
@cjnevin
cjnevin / Navigator.swift
Last active July 3, 2018 05:22
Elegant completion based navigation
import UIKit
import RxSwift
protocol NavigationMode: NSObjectProtocol { }
class PushNavigation: NSObject, NavigationMode { }
class ModalNavigation: NSObject, NavigationMode { }
class RootNavigation: NSObject, NavigationMode { }
class PopToRootNavigation: NSObject, NavigationMode { }
enum NavigationError {
@cjnevin
cjnevin / Mock.swift
Created February 3, 2018 17:08
Easy to use value mocking
import Foundation
import XCTest
public enum MockError: Error {
case error
}
public class Mock<T> {
public enum Count {
case toBeZero
@cjnevin
cjnevin / AlertableView.swift
Created February 4, 2018 22:08
Testable view that presents an alert
import Foundation
import RxSwift
import Action
struct Alert {
struct Option {
enum Style {
case cancel
case destructive
case `default`
@cjnevin
cjnevin / ClosableView.swift
Created February 4, 2018 22:09
View that can be shown modally with a close button in the left corner.
import Foundation
import Action
protocol ClosableView: class {
var closeBarButtonItem: UIBarButtonItem? { get set }
func setCloseAction(_ action: CocoaAction)
func setCloseTitle(_ title: String)
}
private struct AssociatedKeys {
@cjnevin
cjnevin / TitlableView.swift
Created February 4, 2018 22:10
Set navigation title of a view controller
import UIKit
protocol TitlableView {
func setTitle(_ title: String)
}
extension TitlableView where Self: UIViewController {
func setTitle(_ title: String) {
navigationItem.title = title
}
@cjnevin
cjnevin / AppDelegate.swift
Last active February 14, 2018 20:25
Example Redux implementation utilising Rx
import UIKit
import RxSwift
struct AppState {
let countState: CountState
}
struct AppReducer: Reducible {
let countReducer = CountReducer()
@cjnevin
cjnevin / FBSnapshotTestCase+Extensions.swift
Created February 16, 2018 17:20
Extension that allows testing of different screen sizes
import FBSnapshotTestCase
import UIKit
protocol SnapshotView {
var view: UIView! { get }
}
extension UIView: SnapshotView {
var view: UIView! { return self }
}
@cjnevin
cjnevin / UIView+SnapKit.swift
Created February 16, 2018 21:35
Extension on UIView addSubview method to support passing in constraints.
import UIKit
import SnapKit
extension UIView {
func addSubview(_ child: UIView, constraints: (ConstraintMaker) -> ()) {
addSubview(child)
child.snp.makeConstraints(constraints)
}
}
@cjnevin
cjnevin / UIKit+DaisyChain.swift
Last active March 13, 2018 22:09
Daisy Chaining UIKit
protocol Initializable {
init()
}
func createInstance<T>(_ type: T.Type) -> T where T: Initializable {
return type.init()
}
extension UIView: Initializable { }