Skip to content

Instantly share code, notes, and snippets.

View wildthink's full-sized avatar

Jason Jobe wildthink

  • 04:44 (UTC -04:00)
View GitHub Profile
@wildthink
wildthink / QuickTemplate.h
Created August 15, 2018 03:50
Quick Template
//
// QuickTemplate.h
// QuickTemplate
//
// Created by Jason Jobe on 9/21/13.
// Copyright (c) 2013 Jason Jobe. All rights reserved.
//
/*
QuickTemplate uses delimters '<' and '>' to indicate and bracket template commands. It borrows the
@wildthink
wildthink / Clonable.swift
Last active February 16, 2020 14:33
Quick clone of NSObjects using NSArchiver
// https://medium.cobeisfresh.com/accessing-types-from-extensions-in-swift-32ca87ec5190
public protocol Clonable {
func clone() throws -> Self?
}
public protocol Clonable {
func clone() throws -> Self
}
@wildthink
wildthink / DomainContext.swift
Last active February 24, 2019 19:32
Dynamic dependency injection
//
// DomainContext.swift
//
// Copyright (c) 2019 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/fa824de45aacc2fed89aae0fe2cf0370
//
// Created by Jason Jobe on 5/11/15.
// Derived from Relay.swift
// Copyright (c) 2015, 2016 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/acfdf82c2625dc73ad6e42d399d91846
@wildthink
wildthink / FileObserver.swift
Created March 11, 2019 14:22
Monitor updates to files is iOS and OSX
import Foundation
// thanks to https://www.swiftbysundell.com/posts/a-deep-dive-into-grand-central-dispatch-in-swift
/// The FileObjserver watches a single File URL but only if the file exists
/// If the file is removed, you will get a delete event but nothing else
/// thereafter, even if the file is recreated at the same path
public class FileObserver {
public typealias Event = DispatchSource.FileSystemEvent
public let url: URL
public var currentEvent: Event {
@wildthink
wildthink / Resource.swift
Last active November 15, 2019 22:11
Volatile Resource Wrapper inspired by Siesta
//
// Resource.swift
//
// Copyright (c) 2019 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/567608a37253da877eb6a2f69d7e99fc
// Inspired by https://bustoutsolutions.github.io/siesta/
// Created by Jason Jobe on 5/11/19.
//
import Foundation
import Foundation
struct Maker {
var code: () -> Any
func make<A: Any>() -> A? {
return code() as? A
}
}
extension Maker {
@wildthink
wildthink / CodableExtension.swift
Created March 8, 2020 01:21 — forked from sukov/CodableExtension.swift
Codable (Encode & Decode) [Any] and [String: Any] Swift 5
// Original: https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
import Foundation
private struct JSONCodingKeys: CodingKey {
var stringValue: String
init(stringValue: String) {
self.stringValue = stringValue
}
@wildthink
wildthink / Swift Structs vs Parameterized Enums.md
Last active March 30, 2020 20:34
Swift Structs vs Paramaterized Enums

Swift Structs vs Parameterized Enums

There seems to be preference for choosing enums with parameters over Structs. Personally I've always been annoyed by inability of different components to extend an "enumerated" set of values, not to mention the extra work to extract enumerated values as needed (but explained here).

Both Swift structs and enums create a namespace that guides autocompletion. But what I really like about using structs is the ability to easily prototypes, instances providing default values.

With the addition of callAsFunction(...) building on prototypes is even easier.

@wildthink
wildthink / CollectionGroupBy.swift
Created March 19, 2020 15:49
Collection grouping with order preservation
// https://www.goranbrl.dev/posts/1-grouping/
extension Collection {
func groupBy<GroupingType: Hashable>(key: (Element) -> (GroupingType)) -> [[Element]] {
var groups: [GroupingType: [Element]] = [:]
var groupsOrder: [GroupingType] = []
forEach { element in
let key = key(element)
@wildthink
wildthink / .lldbinit
Created April 9, 2020 17:31 — forked from maxchuquimia/.lldbinit
My .lldbinit
command alias objc expression -l objc -O --
command regex swift 's#(.+)#expression -l Swift -O -- defer { CATransaction.flush() }; %1#'
breakpoint set -n AppDelegate.application --one-shot true --auto-continue true
breakpoint command add
swift import Foundation
swift import UIKit
swift import MyApp_tvOS
swift import MyApp_iOS
swift func $printSubviews(of view: UIView) { print(view.perform("recursiveDescription")!) }