Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
wh1pch81n / SingletonSubclassing.swift
Last active February 19, 2022 02:01
The Swift way of doing Singleton inheritance.
import UIKit
class Singleton {
class func sharedInstance() -> Singleton {
struct __ { static let _sharedInstance = Singleton() }
return __._sharedInstance
}
var name: String = String()
}
@wh1pch81n
wh1pch81n / UIAlertController.swift
Created March 2, 2017 04:17
A helper method that will tap one of the buttons programmatically.
// Source: http://stackoverflow.com/a/40634752/3400034
extension UIAlertController {
func tapButton(at index: Int, animated: Bool) {
guard Thread.isMainThread else {
DispatchQueue.main.async {
self.tapButton(at: index, animated: animated)
}
return
}
{
"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 / UIKit_Animator_API.swift
Created December 10, 2016 22:18
A wrapper that allows you to set animation and completion handler in a more consistent way
extension UIKit.UIView {
public class Animator {
fileprivate var animatorBlock: ((@escaping () -> (), @escaping (Bool) -> ()) -> ())
fileprivate var animations: () -> () = { _ in }
public init(animatorBlock: @escaping ((@escaping () -> (), @escaping (Bool) -> ()) -> ())) {
self.animatorBlock = animatorBlock
}
@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 / GenericTableView.swift
Last active December 16, 2017 07:51
Sometimes it is easier to make a tableview generic. And just load it with info, rather than creating a whole class that conforms with UITableViewDataSource and UITableViewDelegate
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet var switchview: UISwitch!
@IBOutlet var sliderview: UISlider!
}
class Section {
var count: Int { return cells.count }
@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
{