Skip to content

Instantly share code, notes, and snippets.

{
"word": "foose ball"
}
@wh1pch81n
wh1pch81n / doorPhrase.json
Last active January 5, 2020 09:36
Phrase Dictionary for Siri Shortcut
{
"big": "b",
"big one": "b",
"small": "s",
"small one": "s",
"small garage": "s"
}
@wh1pch81n
wh1pch81n / Codable Helpers
Created May 23, 2018 04:22
Helper methods that wrap the swift Codable protocol
extension Decodable {
// Converts a dictionary to the desired type
static func decode(dictionary: [String: Any]) throws -> Self {
let data = try JSONSerialization.data(withJSONObject: dictionary, options: [])
return (try JSONDecoder().decode(Self.self, from: data))
}
}
extension Encodable {
// Turns your object into Data
@wh1pch81n
wh1pch81n / git_functions.sh
Created August 3, 2017 12:43
quick git commands
# Checkouts out all branches based on arg name. e.g. master
function git_checkout_all { CMD="git checkout $1 && git submodule foreach 'git checkout $1'"; eval $CMD; }; git_checkout_all master;
# Make branch for all e.g. feature
function git_branch_all { CMD="git branch $1 && git submodule foreach 'git branch $1'"; eval $CMD; }; git_branch_all feature;
# Commit all
function git_commit_all { CMD="git submodule foreach 'git commit -am \"$1\" || echo \"\"' && git commit -am \"$1\""; eval $CMD; }; git_commit_all "msg";
@wh1pch81n
wh1pch81n / gist:85dada72c9f1c56c94a0d0aea7da95c5
Created June 29, 2017 01:45
A Domain specific UserDefaults wrapper
// This class helps facilitate using non-standard defaults.
// Typically standardDefaults will save into "<bundle_identifier>.plist"
// You can use a different one. The BundleUserDefaults class will save into "BundleUserDefaults.plist"
// This means you can clear it without effecting the standard one.
// Base class that all user defaults inherit from
class MyUserDefaults: NSObject {
public let suiteName: String
public let user_default: UserDefaults
init(suiteName: String) {
@wh1pch81n
wh1pch81n / trigger_UITapGestureRecognizer_ofView
Created May 31, 2017 16:36
triggering a UITapGestureRecognizer of a view
for gr in view.gestureRecognizers ?? [] where gr is UITapGestureRecognizer
{
let tgr = gr as! UITapGestureRecognizer
if let _targets = tgr.value(forKey: "targets") as? NSArray {
let targets = _targets as [AnyObject]
for targetAction in targets {
if let desc = targetAction.value(forKey: "description") as? String
, let actionString = desc.components(separatedBy: ",")[0].components(separatedBy: "=").last
, let target = targetAction.value(forKey: "target") as? NSObject
{
@wh1pch81n
wh1pch81n / polymorphic_currying.swift
Created April 29, 2017 19:14
enums with associated types can enable polymorphic currying. May help with functional programming.
// Polymorphic currying
enum Bar {
case weight(lbs: CGFloat)
}
func foo(_ valueA: String, _ valueB: Int) -> (Bar) -> ()
{
return { _bar in
switch _bar {
@wh1pch81n
wh1pch81n / enumString.swift
Created April 21, 2017 03:56
You can use a static var as if it were an enum case of an enum string.
class EnumString: NSObject {
let rawValue: String
init(rawValue: String = #function) {
self.rawValue = rawValue
}
static var abc: EnumString { return .init() }
static var defg: EnumString { return .init() }
static var hijk: EnumString { return .init() }
@wh1pch81n
wh1pch81n / findView.swift
Created April 21, 2017 03:02
A quick class that could aid in finding a view with a specific identifier. Meant to be used in XCTest unit tests. Might be able to get around the limitations of UI Testing i.e. TableViews with 40000 rows.
class Finder<VIEW_TYPE: UIView> {
var foundView: VIEW_TYPE?
public func findView(with identifier: String) throws {
foundView = findView(with: identifier, parent: UIApplication.shared.keyWindow!)
if foundView == nil {
throw NSError()
}
// A possible technique for testing a json transfers to a struct properly
let json: [String: Any] = [
"alpha" : "aaaa",
"beta" : 98,
"delta" : 23.7
]
// the keys that we care about
enum ABCEnum: String {
case alpha