Skip to content

Instantly share code, notes, and snippets.

View scotteg's full-sized avatar
🏠
Working from home

Scott Gardner scotteg

🏠
Working from home
View GitHub Profile
@scotteg
scotteg / PopoverTipModifier.swift
Last active October 11, 2024 16:54
A SwiftUI modifier to present a tip based on dynamic conditions.
import SwiftUI
import TipKit
/// Encapsulate tip conditions with specified values.
protocol TipCondition {
func evaluate() -> (Binding<Bool>, is: Bool)
}
/// Conform `Binding<Bool>` to `TipCondition` with a default value of `true`.
extension Binding: TipCondition where Value == Bool {
@scotteg
scotteg / UIStoryboard+InstantiateViaNameAndType.swift
Last active March 20, 2019 20:47
Extension on UIStoryboard to simplify programmatic instantiation
import UIKit
extension UIStoryboard {
enum Name: String {
case main = "Main"
case otherStuff = "OtherStuff"
}
@scotteg
scotteg / HandlesKeyboard.swift
Last active March 1, 2019 16:33
Drop-in keyboard handler
//
// HandlesKeyboard.swift
//
// Created by Scott Gardner on 1/14/19.
// Copyright © 2019 Scott Gardner. All rights reserved.
//
import UIKit
protocol HandlesKeyboard where Self: UIViewController {
// From Swift 2 Essential Training http://www.lynda.com/Swift-tutorials/Using-labels/422096/447960-4.html
var i = 0
let hello = "Hello, playground!"
start: do {
i++
do {
print(hello)
@scotteg
scotteg / SwiftCGPointsOnCircle.swift
Last active January 22, 2020 12:57
Returns an array of `CGPoint`s that are evenly distributed around the circumference of a circle for a given radius, center x, center y, and number of decimal points precision.
import UIKit
/**
Returns an array of `CGPoint`s that are evenly distributed around the circumference of a circle for a number of points, center x, center y, and radius of the circle, and maximum number of decimal points precision for the x and y values.
- author: Scott Gardner
- parameter numberOfPoints: the number of points to plot; 1 or more
- parameter centerX: the center `x` of the circle
- parameter centerY: the center `y` of the circle
- parameter radius: the radius of the circle (distance from center)
- parameter precision: the maximum number of decimal places precision, 0 or higher, **defaults to 3**
@scotteg
scotteg / SwiftQueryItemsDictionaryNSURLExtension.swift
Last active November 30, 2015 13:10
Swift NSURL extension that returns a Dictionary of String keys and String values for each key/value pair in an NSURL query string.
import Foundation
/**
Returns a `Dictionary` of `String` keys and `String` values for each key/value pair in an `NSURL` query string.
- author: Scott Gardner
- seealso:
* [Source on GitHub](http://bit.ly/SwiftQueryItemsDictionaryNSURLExtension)
*/
extension NSURL {
@scotteg
scotteg / SwiftArrayOfUIViewsSortInPlaceByTagExtension.swift
Last active April 11, 2020 05:18
Sorts an array of UIViews or subclasses by tag.
extension Array where Element: UIView {
/**
Sorts an array of `UIView`s or subclasses by `tag`. For example, this is useful when working with `IBOutletCollection`s, whose order of elements can be changed when manipulating the views in Interface Builder. Just tag your views in Interface Builder and then call this method on your `IBOutletCollection`s in `viewDidLoad()`.
- author: Scott Gardner
- seealso:
* [Source on GitHub](http://bit.ly/SortUIViewsInPlaceByTag)
*/
mutating func sortUIViewsInPlaceByTag() {
sortInPlace { (left: Element, right: Element) in
@scotteg
scotteg / HashableType.swift
Last active December 16, 2016 15:15
A Swift HashableType that enables nesting a Set of any Hashable type within a Set
func ==(x: HashableType, y: HashableType) -> Bool {
return x.isEqual(y.value)
}
struct HashableType: Hashable {
let value: Any
var hashValue: Int {
return getHashValue()
}
@scotteg
scotteg / SwiftIntIsPrimeExtension.swift
Created August 8, 2015 12:33
A Swift 2 Int extension that checks if an integer is a prime number.
extension Int {
/**
`true` if self is a prime number, i.e., can only be divided evenly by 1 and itself.
- author: Scott Gardner
- seealso:
* [Source on GitHub](http://bit.ly/SwiftIntIsPrimeExtension)
*/
public var isPrime: Bool {
guard self > 1 else {
@scotteg
scotteg / SwiftSpellOutNumber.swift
Last active September 18, 2017 12:04
Converts any number (e.g., 1) or a number string (e.g., "1") into a spelled-out number string (e.g., "one").
import Foundation
/**
Converts any numeric literal (e.g., 1) or string containing a numeric literal (e.g., "1"), into a spelled-out number string (e.g., "one"). [Source on GitHub](http://bit.ly/SwiftSpellOutNumber)
- parameter number: a numeric literal, or string containing a numeric literal
- returns: String?
*/
public func spellOut<N>(number: N) -> String? {
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut