Skip to content

Instantly share code, notes, and snippets.

@shawnthroop
shawnthroop / ContentView.swift
Created October 7, 2022 14:30
A simple test app for demonstrating view update warnings occurring in iOS 16
import SwiftUI
struct ContentView: View {
var body: some View {
RootView()
.modifier(RootViewModifier())
}
}
@shawnthroop
shawnthroop / Keychain.swift
Created May 9, 2022 14:53
A simple and relatively typesafe interface for the Keychain
import Security
import Foundation
import CoreFoundation
typealias KeychainQuery = [CFString: Any]
typealias KeychainQueryItems = [KeychainQuery]
struct KeychainError: Error {
var status: OSStatus
@shawnthroop
shawnthroop / Network.swift
Created December 7, 2019 13:21
A simple type safe wrapper around URLSession and URLRequest.
import Foundation
public enum Method : String, Hashable {
case options, get, head, post, put, delete, trace, connect
}
public struct HeaderField : Hashable {
public let rawValue: String
public init(_ rawValue: String) {
@shawnthroop
shawnthroop / ContentView.swift
Last active May 12, 2020 22:13
A UIScrollView but SwiftUI
import SwiftUI
struct ContentView: View {
@State private var bounds: Bounds = .zero
var body: some View {
VStack {
controls
scrollArea
@shawnthroop
shawnthroop / Path.swift
Created March 14, 2019 18:51
A value type representing components separated by "/"
// swift 4.2
import Foundation
/// A value type representing a path comprized of components separated by "/"
public struct Path: Hashable {
public typealias Component = String
private(set) public var rawValue: String = ""
// swift 4.2
public class MutableVariable<A>: Variable<A> {
public typealias Transform = (inout A) -> Void
internal typealias Mutate = (@escaping Transform) -> Void
override public class func threadSafe(_ initial: A) -> MutableVariable<A> {
return MutableVariable(initial, queue: DispatchQueue(label: "com.shawnthroop.sting.\(self)", attributes: .concurrent))
}
@shawnthroop
shawnthroop / Endpoint.swift
Last active March 12, 2019 08:18
A simple and extensible networking library. Supports posting basic multipart form data and type safe query parameters.
// swift 4.2
public struct Endpoint: Equatable {
public typealias Query = Set<QueryItem>
public typealias Headers = [HeaderField: String]
public enum Method: Equatable {
case get
case post(Body)
@shawnthroop
shawnthroop / Accounts.swift
Last active July 2, 2018 14:35
An elaboration of Swift Talk S01E63 (Mutable Shared Structs). Provides a simpler base class for read-only properties.
// swift 4.1
import Foundation
import Sting
struct Account: Equatable {
var id: Int
}
struct Accounts {
@shawnthroop
shawnthroop / AnyEncodable.swift
Last active May 28, 2018 11:49
Type-erased Equatable and Encodable values
// swift 4.1
/// A type-erased encodable value
public struct AnyEncodable {
public var base: Any {
return value.base
}
private let value: AnyEquatable
private let encodeValue: (Encoder, AnyEquatable) throws -> Void
@shawnthroop
shawnthroop / AnyEquatable.swift
Created May 16, 2018 00:23
A protocol allowing comparison between untyped values
// swift 4.1
protocol AnyEquatable {
func isEqual(to value: Any) -> Bool
}
extension AnyEquatable where Self: Equatable {
func isEqual(to value: Any) -> Bool {
guard let value = value as? Self else {
return false