Skip to content

Instantly share code, notes, and snippets.

View atierian's full-sized avatar

Ian Saultz atierian

  • Charleston, SC
View GitHub Profile
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 / 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
@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]()
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 / String+StrikeThrough.swift
Last active December 23, 2020 14:21
Strike through String while maintaining String type
//Doesn't work with all characters - use cautiously. NSAttributedString is preferred.
extension String {
var strikeThrough: String {
guard let strikeThrough = "\u{0336}".unicodeScalars.first else {
return ""
}
return self.map { char -> String in
var c = UnicodeScalarView(char.unicodeScalars)
c.append(strikeThrough)
return String(c)
@atierian
atierian / String+HTMLStripped.swift
Last active June 24, 2021 16:23
Strip HTML tags / Decode HTML from String
import UIKit
import XCTest
extension String {
var htmlStripped: String? {
data(using: .unicode)
.flatMap {
try? NSAttributedString(
data: $0,
options: [
@propertyWrapper
struct UserDefault<Value> {
let key: String
let defaultValue: Value
var wrappedValue: Value {
get {
(UserDefaults.standard.object(forKey: self.key) as? Value) ?? self.defaultValue
}
set {
@atierian
atierian / Array+GuardedSubscript.swift
Last active July 21, 2021 13:29
Extension on subscript of Array to return nil if subscript is out of range
import UIKit
import XCTest
extension Collection {
subscript(guarded idx: Index) -> Element? {
indices.contains(idx) ? self[idx] : nil
}
}
class GuardedSubscriptTests: XCTestCase {
@atierian
atierian / mirrorPrint.swift
Created August 31, 2020 00:41
Debug print all properties in an object in foo = bar form
public func mirrorPrint(_ object: Any, terminator: String = "\n") {
for child in Mirror(reflecting: object).children {
guard let label = child.label else {
Swift.print(child.value, terminator: terminator)
continue
}
Swift.print(label, "=", child.value, terminator: terminator)
}
}
@atierian
atierian / Codable+UserDefaults.swift
Created August 28, 2020 01:28
Store and retrieve Codable objects in UserDefaults
func object<T: Codable>(_ type: T.Type, with key: String, usingDecoder decoder: JSONDecoder = JSONDecoder()) -> T? {
guard let data = value(forKey: key) as? Data else { return nil }
return try? decoder.decode(type.self, from: data)
}
func set<T: Codable>(object: T, forKey key: String, usingEncoder encoder: JSONEncoder = JSONEncoder()) {
let data = try? encoder.encode(object)
set(data, forKey: key)
}