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 / AsyncToCompletionHandlers.swift
Created November 8, 2021 16:41
Going from async to completion handlers.
import UIKit
enum SomeError: Error {
case foo, bar, baz, unknown
}
struct SDK {
func simpleAsync() async -> Int {
try? await Task.sleep(seconds: 2)
return 42
@atierian
atierian / MyCell.swift
Created October 28, 2021 14:43
Oversimplified Example of MVVM
class MyCell: UITableViewCell {
let titleLabel = UILabel()
let subtitleLabel = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder: NSCoder) { nil }
@atierian
atierian / Publisher+Result.swift
Last active September 30, 2021 12:41 — forked from IanKeen/Publisher+Result.swift
Extension to convert a failable Publisher into a non-failable Result based publisher (as well as extensions to break out values/errors) - Same idea as RxSwifts Materialize
extension Publisher {
func asResult() -> AnyPublisher<Result<Output, Failure>, Never> {
return map(Result.success)
.catch { Just(Result.failure($0)) }
.eraseToAnyPublisher()
}
}
extension Publisher {
func values<S, F: Error>() -> AnyPublisher<S, Never> where Output == Result<S, F>, Failure == Never {
@atierian
atierian / AnyCodingKey.swift
Created September 7, 2021 15:24
Top Level Key Decoding
struct AnyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}
init(intValue: Int) {
@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
@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 / 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 / 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 / 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 {