Skip to content

Instantly share code, notes, and snippets.

View jakehawken's full-sized avatar
🤖
Obsessed with learning Godot right now

Jake Hawken jakehawken

🤖
Obsessed with learning Godot right now
View GitHub Profile
@jakehawken
jakehawken / OptionalAssigment.swift
Created October 11, 2018 22:13
Optional assignment operator. (Discovered after writing this that it had already been thought of, suggested to, and rejected by Swift Evolution)
infix operator ??=
func ??=<T>(lhs: inout T, rhs: T?) {
guard let rhs = rhs else {
return
}
lhs = rhs
}
// This collapses and obviates redundant-looking use of the nil coalescing operator
// so that given this:
Pod::Spec.new do |s|
s.name = '{YOUR POD NAME}'
s.version = '0.0.1'
s.summary = '{HIGH LEVEL, ONE-SENTENCE SUMMARY}'
s.description = <<-DESC
{LONG-FORM DESCRIPTION OF YOUR COCOAPOD}
Tags: {A COMMA-SEPARATED LIST OF SEARCHABLE TAGS}
DESC
# I recommend writing a git hook that scrapes this from your README.md file.
@jakehawken
jakehawken / ToggleableStackView.swift
Last active August 1, 2018 22:36
A way to easily transition between displaying two different sets of subviews in a UIStackView.
import Foundation
import UIKit
class ToggleableStackView: UIStackView {
enum Cohort {
case primary
case secondary
var next: Cohort {
switch self {
case .primary:
@jakehawken
jakehawken / UIImage+Color.swift
Created July 17, 2018 20:58
Quickly generate images of programmatic colors on the fly.
extension UIImageView
func setImage(withColor color: UIColor) {
image = UIImage.withColor(color, size: bounds.size)
}
}
extension UIImage {
static func withColor(_ color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(origin: CGPoint.zero, size: size)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
@jakehawken
jakehawken / AlmostIncreasingSequence.swift
Created April 30, 2018 01:44
For the "almostIncreasingSequence" problem on CodeFights:
func almostIncreasingSequence(sequence: [Int]) -> Bool {
let count = sequence.count
if count < 3 {
return true
}
if sequence.sorted() == sequence {
return isStrictlyAscending(array: sequence)
}
var mutableSequence = sequence
@jakehawken
jakehawken / AsSetAsArray.swift
Created March 14, 2018 04:54
.asSet() and .asArray() -- Why don't these already exist?
extension Array where Element: Hashable {
func asSet() -> Set<Element> {
return Set(self)
}
}
extension Set {
func asArray() -> [Element] {
return Array(self)
}
@jakehawken
jakehawken / MapToDictionary.swift
Created March 14, 2018 04:40
A block-based approach to inflating an array into a dictionary.
extension Array {
func mapToDictionary<Key:Hashable,Value>(mapBlock: (Element)->(Key,Value)) -> [Key:Value] {
var dict = [Key:Value]()
self.forEach { (element) in
let kvTuple: (Key,Value) = mapBlock(element)
dict[kvTuple.0] = kvTuple.1
}
return dict
}
}
@jakehawken
jakehawken / EmptyOrNil.swift
Last active March 12, 2018 18:23
I have wanted this method to exist since Swift 1. I realized this morning that there was no reason that it couldn't.
extension Optional where Wrapped: Collection {
/*
NOTE: Since this extends Optional rather than the possible collection,
you don't use the "?" when calling it `isEmptyOrNil()`
Example:
var myString: String? = ""
myString.isEmptyOrNil // Evaluates to true.
myString = nil
myString.isEmptyOrNil // Also evaluates to true.
myString?.isEmptyOrNil // Will not compile.
@jakehawken
jakehawken / UITableView+Helpers.swift
Created February 15, 2018 20:19
Don't you wish UIRefreshControl used blocks? Don't you wish you could add one to a TableView with one line of code?
import UIKit
class BlockRefreshControl: UIRefreshControl {
private let block: ()->()
init(block: @escaping ()->()) {
self.block = block
super.init()
addTarget(self, action: #selector(BlockRefreshControl.peformBlock), for: .valueChanged)
}
@jakehawken
jakehawken / StoryboardInitalVC.swift
Last active February 14, 2018 15:54
A means of quickly giving a UIViewController class-level functionality for being instantiated as the initial VC of a Storyboard of the same name.
extension UIStoryboard {
static func instantiateInitialVC<T: UIViewController>() -> T {
let className = String(describing: T.self)
return instantiateInitialVC(storyboardName: className)
}
static func instantiateInitialVC<T: UIViewController>(storyboardName: String) -> T {
let className = String(describing: T.self)
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
guard let instance = storyboard.instantiateInitialViewController() as? T else {