Skip to content

Instantly share code, notes, and snippets.

👨‍💻
Coding!

Andrew Crookston acrookston

👨‍💻
Coding!
Block or report user

Report or block acrookston

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@acrookston
acrookston / draft.swift
Last active Jan 23, 2019
Exploring view/controller state with Loading / Content / Error (LCE)
View draft.swift
/// 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 Jan 10, 2019
Apple Notes to Evernote migration import/export script
View notes migration.scpt
# 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
View String+Extensions.swift
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 Apr 28, 2018
Simple SQLite migration for Swift with the SQLite.swift framework
View MigrationCreateItems.swift
//
// 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 Feb 19, 2018
This code takes several minutes (3min on MacBook Pro i5 2.4Ghz) to compile
View fast_code.swift
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)
View UIColorExtensions.swift
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)
}
}
View TimeIntervalExtensions.swift
extension TimeInterval {
var milliseconds: Int {
return Int(truncatingRemainder(dividingBy: 1) * 100)
}
var minutes: Int {
return Int(self / 60)
}
var seconds: Int {
View DateExtensions.swift
//
// 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 Apr 27, 2019
trim, strip and split, regex for Swift String
View NSRegularExpression.swift
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)
You can’t perform that action at this time.