Skip to content

Instantly share code, notes, and snippets.

View atierian's full-sized avatar

Ian Saultz atierian

  • Charleston, SC
View GitHub Profile
@atierian
atierian / EmailDetector.swift
Last active August 27, 2021 18:03
Email Data Detector
// Inspired by https://www.atomicbird.com/blog/data-detection-in-swift/
import Foundation
import XCTest
fileprivate extension URLComponents {
init?(_ url: URL) {
self.init(url: url, resolvingAgainstBaseURL: false)
}
import Foundation
import XCTest
extension MutableCollection {
/// Iterates through a mutable collection and mutates values based on the closure argument
/// - Parameter closure: Takes and element and mutates it
mutating func mutatingForEach(_ closure: (inout Element) -> Void) {
indices.forEach {
closure(&self[$0])
import Foundation
import XCTest
infix operator >?< : DefaultPrecedence
extension Date {
/// Generate a random date between two dates - Dates do not need to be in order
/// - Parameters:
/// - start: first date
/// - end: second date
/// - Returns: Random `Date` between two dates
@atierian
atierian / Fonts.swift
Created June 21, 2021 17:14
Two approaches to seeing installed fonts
import UIKit
import PlaygroundSupport
// Print fonts
extension UIFont {
public static func printAllFonts() {
for family in UIFont.familyNames.sorted() {
let names = UIFont.fontNames(forFamilyName: family)
print("Family: \(family)")
names.forEach {
@atierian
atierian / Array+RemoveDuplicatesByKeyPath.swift
Created July 13, 2021 12:09
Remove duplicates determined KeyPath
import UIKit
import PlaygroundSupport
import XCTest
extension Array {
/// Remove duplicates based on `KeyPath`
/// - Parameter keyPath: Property to determine uniqueness
/// - Returns: `Array` of unique `Elements` based on `keyPath` argument
/// - Complexity Time: O(n) / Space: O(n)
func removingDuplicates<T: Hashable>(by keyPath: KeyPath<Element, T>) -> Self {
@atierian
atierian / CodableUserDefaultsPropertyWrapper.swift
Created July 13, 2021 12:13
Codable UserDefaults PropertyWrapper
@propertyWrapper
struct CodableUserDefault<Value: Codable> {
let key: String
var wrappedValue: Value? {
get {
UserDefaults.standard.data(forKey: key).flatMap {
try? JSONDecoder().decode(Value.self, from: $0)
}
}
@atierian
atierian / NetworkLogger.swift
Created July 13, 2021 19:31
Network Logger
import Foundation
public protocol NetworkLoggable {
func log<T: CustomStringConvertible>(
label: String,
value: T?,
level: NetworkLogger,
function: StaticString,
line: UInt,
file: String
@atierian
atierian / insetGroupedValues.txt
Created August 10, 2021 18:51
UITableView insetGrouped
Top: 35
Sides: 20
Corner Radius: 10
(lldb) po tableView.cellForRow(at: .init(row: 0, section: 0))?.frame
▿ Optional<CGRect>
▿ some : (0.0, 35.0, 374.0, 44.0)
▿ origin : (0.0, 35.0)
- x : 0.0
- y : 35.0
@atierian
atierian / StateMachines.swift
Created August 31, 2021 14:14
Building State Machines in Swift
/*
Cory Benfield - Building State Machines in Swift
https://www.youtube.com/watch?v=7UC7OUdtY_Q
What is a Finite State Machine?
- Structured way to represent computation
- System can be in one of a finite number of states at any time
- Reacts to inputs by changing state, and optionally producing a side effect
- Deterministic and nondeterministic flavors
- Simple model of computation: easy to understand