Skip to content

Instantly share code, notes, and snippets.

View qmchenry's full-sized avatar

Quinn McHenry qmchenry

View GitHub Profile
@qmchenry
qmchenry / DumpView.swift
Last active February 22, 2022 16:57
dump() example
// DumpExample
// This SwiftUI View explores use of the dump() function for manipulating the debugging display
// of large, structured data.
//
// Created by Quinn McHenry on 2/17/22.
import SwiftUI
struct ContentView: View {
@qmchenry
qmchenry / PreviewHelpers.swift
Created August 5, 2021 17:00
Decodable instantiation from an asset catalog Data Set to help with SwiftUI Previews
import UIKit
extension NSDataAsset {
/// Load a data asset from a catalog and try decoding it to a specific Decodable type
/// - Returns: An optional instance of type T decoded from the named data asset
/// Usage: `NSDataAsset.decode(DocodableThing.self, asset: "my thing")`
public static func decode<T>(_ type: T.Type, asset name: String) -> T? where T: Decodable {
guard let asset = Self(name: name) else { return nil }
return try? JSONDecoder().decode(T.self, from: asset.data)
}
@qmchenry
qmchenry / git-switcher
Created November 9, 2020 17:42
git interactive branch switcher
#!/bin/bash
select branch in $(git branch | cut -c3-)
do
git switch $branch
exit
done
@qmchenry
qmchenry / String+Bundle.swift
Created April 17, 2019 00:27
PlanetSwift bundlePath
//
// String+Bundle.swift
// PlanetSwift
//
// Created by Brad Bambara on 1/13/15.
// Copyright (c) 2015 Small Planet. All rights reserved.
//
import Foundation
,ad8888ba, 88 88
d8"' `"8b ,d 88 ""
d8' `8b 88 88
88 88 MM88MMM 88,dPPYba, ,adPPYba, 8b,dPPYba, 8b db d8 88 ,adPPYba, ,adPPYba,
88 88 88 88P' "8a a8P_____88 88P' "Y8 `8b d88b d8' 88 I8[ "" a8P_____88
Y8, ,8P 88 88 88 8PP""""""" 88 `8b d8'`8b d8' 88 `"Y8ba, 8PP"""""""
Y8a. .a8P 88, 88 88 "8b, ,aa 88 `8bd8' `8bd8' 88 aa ]8I "8b, ,aa
`"Y8888Y"' "Y888 88 88 `"Ybbd8"' 88 YP YP 88 `"YbbdP"' `"Ybbd8"'
@qmchenry
qmchenry / Lion.svg
Last active June 12, 2017 11:38
Lion.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@qmchenry
qmchenry / enumCaseName.swift
Created April 21, 2017 18:07
Swift enum name string with associated values
// Swift enums cannot be declared with a rawValue if they have associated values
// like good ol' Amargasaurus has. Using String(describing: dino) on a case with
// associated also includes the values, but works fine on unassociated cases.
// Mirror(reflecting: dino) can extract the name of an associated case, but is
// nil for unassociated cases. Our hero ?? swoops in to save the day!
enum Sauropoda {
case Amargasaurus(things: Int64, hasOtherThing: Bool?)
case Antetonitrus
// ...
@qmchenry
qmchenry / ThenExample.swift
Created September 17, 2016 18:53
Then and now
// with Then
let label = UILabel().then {
$0.textAlignment = .center
$0.textColor = .black
$0.text = "Hello, World!"
}
// just Swift
let label: UILabel() = {
$0.textAlignment = .center
@qmchenry
qmchenry / CollectionType+subscript.swift
Created July 11, 2016 18:53
Swift subscript tricks
// http://stackoverflow.com/questions/25329186/safe-bounds-checked-array-lookup-in-swift-through-optional-bindings/30593673#30593673
extension CollectionType {
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
subscript (safe index: Index) -> Generator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
// Usage:
@qmchenry
qmchenry / ToSwift3.swift
Last active June 29, 2016 12:26
Example of new Swift 3 swiftier GCD syntax
// Swift 2.x
public class func GCDDelay(delayAmount:Float, block:(Void->Void)) {
dispatch_after(dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW),
Int64(delayAmount * Float(NSEC_PER_SEC))), dispatch_get_main_queue(), block)
}
GCDDelay(0.5) { UIApplication.sharedApplication.openURL(url) }
// Swift 3