Skip to content

Instantly share code, notes, and snippets.

View feighter09's full-sized avatar

Austin Feight feighter09

View GitHub Profile
@feighter09
feighter09 / Weak.swift
Last active October 1, 2019 14:23
A helper to weakly capture an object in a block
func weak<Object: AnyObject>(_ object: Object, block: @escaping (Object) -> Void) -> () -> Void {
return { [weak object] in
guard let object = object else { return }
block(object)
}
}
func weak<Object: AnyObject, Input>(_ object: Object, block: @escaping (Object, Input) -> Void) -> (Input) -> Void {
return { [weak object] in
guard let object = object else { return }
import Foundation
struct Person {
var firstName: String
var lastName: String
var age: Int
}
extension Person {
enum Query {
override func spec()
{
var subject: UIViewController!, viewModel: ViewModel!
beforeEach { (subject, viewModel) = self.newSubject }
context("once the view has loaded") {
beforeEach { subject.loadViewIfNeeded() }
describe("events sent to view model") {
context("when selection changes") {
@feighter09
feighter09 / TableViewCellType.swift
Created March 28, 2017 05:37
Make tableviews a bit safer
protocol CellType {
static var identifier: String { get }
}
extension CellType {
static var identifier: String { return "\(Self.self)" }
}
extension UITableView {
func register<Cell>(forReuse cell: Cell.Type)
tableView.register(forReuse: StoryCell.self)
let cell = tableView.dequeueReusable(StoryCell.self, for: indexPath)
tableView.register(StoryCell.self, forCellReuseIdentifier: "StoryCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "StoryCell", for: indexPath) as? StoryCell
let defaults = Defaults()
let hasLoggedIn = true
defaults.save(true, forKey: .LoggedIn)
defaults.save(false, forKey: .NotificationsEnabled)
let regularOrder = Order(item: "Cold Brew", size: .Large)
let alternateOrder = Order(item: "Latte", size: .Small)
defaults.save(regularOrder)
defaults.save(alternateOrder, forKey: .Alternate)
let defaults = Defaults()
let person = Person(firstName: "Austin", lastName: "Feight", age: -3)
defaults.save(person)
let fetched: Person? = defaults.fetch(Person.self)
print("\(fetched!.firstName) \(fetched!.lastName) is \(fetched!.age) years old")
// Output: Austin Feight is -3 years old
extension Person: DefaultConvertible {
private static let firstNameKey = "first_name"
private static let lastNameKey = "last_name"
private static let ageKey = "age"
var serialized: [String : AnyObject] {
return
[
Person.firstNameKey: firstName as AnyObject,
Person.lastNameKey: lastName as AnyObject,
struct Person {
var firstName: String
var lastName: String
var age: String
}
let person = Person(firstName: "Austin", lastName: "Feight", age: -3)
UserDefaults.standard.set(person)
let fetchedPerson = UserDefaults.standard.fetch(Person.self)