Skip to content

Instantly share code, notes, and snippets.

View atierian's full-sized avatar

Ian Saultz atierian

  • Charleston, SC
View GitHub Profile
var versionLabelText: String {
guard let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
else { return "" }
return "App Version: " + version + "." + build
}
@atierian
atierian / TimeZoneDisplay.swift
Created February 22, 2021 22:01
TimeZones Split by Daylight Savings Time Participants
struct TimeZoneDisplay {
struct TimeZone {
let abbreviation: String
let description: String
let isDaylightSavingsParticipant: Bool
}
var timeZones = [TimeZone]()
@atierian
atierian / RSA.swift
Created March 23, 2021 13:17
Basic example showing how RSA encryption works
/// Select two prime numbers `p` and `q` (generally very large numbers, but using small numbers due to limitations of UInt)
let p: UInt = 5
let q: UInt = 7
/// `n` is the product of `p` and `q`
let n: UInt = p * q //35
/// `r` is `(p - 1) * (q - 1)`
let r: UInt = (p-1) * (q-1) //24
import Foundation
import XCTest
infix operator ^^ : AdditionPrecedence
extension Int {
/// Add `n` to each digit, exclude carry. e.g.
/// ~~~
/// let shifted = 1945.shiftedUp(by: 2) // 3167
/// ~~~
@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
}
@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 / 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 / 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 / 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 / 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 {