Skip to content

Instantly share code, notes, and snippets.

View acrookston's full-sized avatar
👨‍💻
Coding!

Andrew Crookston acrookston

👨‍💻
Coding!
View GitHub Profile
@acrookston
acrookston / draft.swift
Last active January 23, 2019 21:01
Exploring view/controller state with Loading / Content / Error (LCE)
/// ContentLoader is a lightweight state manager for loading and displaying content and/or errors.
/// It allows you to repeatedly make requests and display new content or cached content in the event of an error.
/// Commonly known as RemoteData or LCE (Loading / Content / Error).
/// Inspired by https://tech.instacart.com/lce-modeling-data-loading-in-rxjava-b798ac98d80
///
final class ContentLoader<T> {
init() { }
@discardableResult func loader(_ loader: (() -> Void)?) -> ContentLoader<T> {
self.loader = loader
@acrookston
acrookston / notes migration.scpt
Created January 10, 2019 18:14
Apple Notes to Evernote migration import/export script
# Thanks to: https://medium.com/doraoutloud/migrating-your-apple-notes-to-evernote-12114418ba00
# Open "Script Editor" application, paste and run.
tell application "Notes"
set theMessages to every note
repeat with thisMessage in theMessages
set myTitle to the name of thisMessage
extension String {
func localized(_ args: CVarArg...) -> String {
return String(format: NSLocalizedString(self, comment: ""), arguments: args)
}
}
let format = NSLocalizedString("Hello %@", comment: "")
print(String(format: format, "World"))
@acrookston
acrookston / MigrationCreateItems.swift
Last active April 28, 2018 22:09
Simple SQLite migration for Swift with the SQLite.swift framework
//
// MigrationCreateItems.swift
// Stash
//
// Created by Andrew C on 8/30/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
// License: MIT
//
import Foundation
@acrookston
acrookston / fast_code.swift
Last active February 19, 2018 03:37
This code takes several minutes (3min on MacBook Pro i5 2.4Ghz) to compile
struct Test {
let computed: Int
init(a: Int, b: Int, c: Int, d: Int, e: Int = 0, f: Int = 0, g: Int = 0, h: Int = 0) {
var value: Int = (a << 56)
value |= (b << 48)
value |= (c << 40)
value |= (d << 32)
value |= (e << 24)
value |= (f << 16)
value |= (g << 8)
@acrookston
acrookston / UIColorExtensions.swift
Created February 15, 2018 21:28
UIColor Extensions
extension UIColor {
static func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
}
@acrookston
acrookston / TimeIntervalExtensions.swift
Created February 15, 2018 21:27
TimeInterval extensions
extension TimeInterval {
var milliseconds: Int {
return Int(truncatingRemainder(dividingBy: 1) * 100)
}
var minutes: Int {
return Int(self / 60)
}
var seconds: Int {
@acrookston
acrookston / DateExtensions.swift
Created February 7, 2018 18:26
Swift Date helpers
//
// CoreExtensions
//
// Created by Andrew Crookston on 1/30/18.
//
import Foundation
public extension DateComponents {
public static func with(year: Int? = nil, month: Int? = nil, day: Int? = nil, hour: Int? = nil, minute: Int? = nil, second: Int? = nil, calendar: Calendar = .current, timeZone: TimeZone? = nil) -> DateComponents {
@acrookston
acrookston / NSRegularExpression.swift
Last active April 5, 2021 06:50
trim, strip and split, regex for Swift String
extension NSRegularExpression {
convenience init(substrings: [String], options: NSRegularExpression.Options) throws {
let escapedSubstrings: [String] = substrings.map(NSRegularExpression.escapedTemplate)
let pattern: String = escapedSubstrings.joined(separator: "|")
try self.init(pattern: pattern, options: options)
}
convenience init?(with pattern: String, options: NSRegularExpression.Options = []) {
do {
try self.init(pattern: pattern, options: options)