Skip to content

Instantly share code, notes, and snippets.

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

Andrew Crookston acrookston

👨‍💻
Coding!
View GitHub Profile
//
// LICENSE: MIT
// Created by Andrew C on 4/25/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
//
class TopAlignedLabel: UILabel {
override func drawText(in rect: CGRect) {
if let string = text as NSString? {
let size = string.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude),
@acrookston
acrookston / BackgroundColorButton.swift
Last active February 15, 2018 18:14
A iOS UIButton subclass with support for background colors
//
// LICENSE: MIT
// Source: https://gist.github.com/acrookston/af50c921089b93d1d71e7a9349dc7af7
// Created by Andrew C on 4/16/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
//
import UIKit
extension UIControlState: Hashable {
//: [Previous](@previous)
import Foundation
var input = """
626 2424 2593 139 2136 163 1689 367 2235 125 2365 924 135 2583 1425 2502
183 149 3794 5221 5520 162 5430 4395 2466 1888 3999 3595 195 181 6188 4863
163 195 512 309 102 175 343 134 401 372 368 321 350 354 183 490
2441 228 250 2710 200 1166 231 2772 1473 2898 2528 2719 1736 249 1796 903
3999 820 3277 3322 2997 1219 1014 170 179 2413 183 3759 3585 2136 3700 188
input = "3893445835429722678558456317563893861752455542588369533636585887178232467588827193173595918648538852463974393264428538856739259399322741844613957229674619566966921656443476317729968764183945899765294481327998956154956571467872487576314549468261122281384513266834769436913544431258253346374641589492728885222652146158261225296144835682556133922436438188211288458692217737145834468534829945993366314375465767468939773939978272968388546791547526366348163672162245585168892858977723516752284597322176349412485116173844733679871253985762643852151748396593275274582481295864991886985988427966155944392352248314629138972358467959614279553511247863869663526823326467571462371663396188951696286916979923587358992127741723727623235238531991996999181976664226274715591531566495345212849683589582225465555847312199122268773923175183128124556249916458878785361322713513153175157855597289482439449732469754748544437553251412476225415932478849961897299721228198262823515159848941742786272262236888514421279147329383465929358896761
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2017 Jean-David Gadina - www.xs-labs.com
* Original: https://github.com/macmade/user-defaults/blob/master/swift/Preferences.swift
*
* Copyright (c) 2017 Andrew Crookston - andrewcrookston.com
* Contributor: https://gist.github.com/acrookston/f973137b9adbd9bff3589ca2cd1d1cb8
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
// Results:
// 0.40788197517395 - IteratedEnum (it's slow due to EnumCollection)
// 0.282593011856079 - StaticIteratedEnum
// 0.152174055576324 - SwitchedEnum
// 0.159240961074829 - OtherSwitchedEnum
import Foundation
// MARK: - Benchmark - gotta know how fast it is!
@acrookston
acrookston / CallbackViewModel.swift
Last active September 6, 2017 20:18
Demonstrating MVVM View Model with multiple observers in Swift
class CallbackViewModel {
typealias DidUpdateCallback = ((Void) -> Void)
private var didUpdateCallbacks = [DidUpdateCallback?]()
// Have to remember to use `[weak self] in` in your callback!
func addObserver(_ callback: @escaping DidUpdateCallback) {
didUpdateCallbacks.append(callback)
}
@acrookston
acrookston / git-purge.sh
Last active May 19, 2017 18:30
Deletes local and remote branch. Add as alias in .gitconfig `purge = "!git-purge $1"`
#!/bin/sh
if [ -z "$1" ]
then
echo "Usage: git-purge [branch]"
exit
fi
if [ "master" == "$1" ]
then
@acrookston
acrookston / roman_numeral.swift
Created May 9, 2017 00:24
Convert roman numerals to integers
import Foundation
let values: [Character: Int] = [
"I" : 1,
"V" : 5,
"X" : 10,
"L" : 50,
"C" : 100,
"D" : 500,
"M" : 1000