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 / 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 {
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
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])
@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)
}
@atierian
atierian / DataCompression.swift
Last active August 5, 2022 20:18
Compressing and Decompressing Data
import Foundation
import Compression
import XCTest
extension Encodable {
/// Returns a string object describing the encodable object's byte size using `ByteCountFormatter`. Or a description of the `DecodingError` generated during the decoding process
var size: String {
do {
return try JSONEncoder().encode(self).size
} catch {
@atierian
atierian / KeyPathBinarySearch.swift
Last active June 19, 2021 18:39
Binary Search by KeyPath
import UIKit
import XCTest
extension Array {
/// Determine if an `Array` contains an element by a specific property using a Binary Search Algorithm
/// - Parameters:
/// - searchItem: Element with property to search for
/// - keyPath: KeyPath of property to search for
/// - isSorted: Whether or not the `Array` is ordered. Default value is `true`
/// - Returns: `true` if `Array` contains `searchItem` KeyPath, `false` if it does not
@atierian
atierian / CopyOnWrite.swift
Last active April 11, 2021 16:20
Struct with copy on write semantics
struct Person {
fileprivate final class Storage {
var name: String
var age: UInt8
init(name: String, age: UInt8) {
self.name = name
self.age = age
}
}
@atierian
atierian / DumbTableViewCells.swift
Created April 9, 2021 18:39
Multiple UITableViewCell subclasses no logical branches in View objects
// Inspiration from TweetleDumb by Ian Keen
final class FooCell: UITableViewCell {
let label = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
layout()
}
@atierian
atierian / Versioned.swift
Created April 9, 2021 18:32
Property Wrapper for debugging. Maintains history with timestamp
@propertyWrapper
struct Versioned<Value> {
private var currentValue: Value
private(set) var history = VersionHistory()
init(wrappedValue: Value) {
self.currentValue = wrappedValue
}
var wrappedValue: Value {
@atierian
atierian / BitMath.swift
Last active June 19, 2021 18:49
Break down Ints and do addition at the bit level
import Foundation
import XCTest
enum Bit: Int, CustomDebugStringConvertible {
case zero = 0
case one
var debugDescription: String {
rawValue.description
}