Skip to content

Instantly share code, notes, and snippets.

Avatar

Oliver Atkinson ollieatkinson

View GitHub Profile
@ollieatkinson
ollieatkinson / AnyEquatable.swift
Last active Mar 6, 2022
AnyEquatable using _openExistential
View AnyEquatable.swift
public func isEqual(_ x: Any, _ y: Any) -> Bool {
func f<LHS>(_ lhs: LHS) -> Bool {
switch Wrapper<LHS>.self {
case let p as AnyEquatable.Type:
return p._isEqual(x, y)
case let c as AnyEquatableContainer.Type:
return c._isEqual(x, y)
default:
return false
}
@ollieatkinson
ollieatkinson / Navigation.swift
Last active Mar 3, 2022
NavigationRoute for Swift Composable Architecture
View Navigation.swift
import ComposableArchitecture
import SwiftUI
public struct RouteIntent<R: NavigationRoute>: Hashable {
public enum Action: Hashable {
case navigateTo
case enterInto(EnterIntoContext = .default)
}
@ollieatkinson
ollieatkinson / Publishers+RetryDelay.swift
Last active Jun 21, 2022
Add a delay in-between each Combine retry, offering a timing function and default implementations for none, constant seconds and exponential backoff
View Publishers+RetryDelay.swift
import Combine
import Foundation
extension Publisher {
public func retry<S: Scheduler>(
_ max: Int = Int.max,
delay: Publishers.RetryDelay<Self, S>.TimingFunction,
scheduler: S
) -> Publishers.RetryDelay<Self, S> {
@ollieatkinson
ollieatkinson / Executable.swift
Created Aug 17, 2021
Simply execute shell commands from Swift
View Executable.swift
public struct Executable {
let url: URL
public init(_ filePath: String) {
url = URL(fileURLWithPath: filePath)
}
public init(_ url: URL) {
self.url = url
@ollieatkinson
ollieatkinson / CombineLatestCollection.swift
Last active Jun 29, 2021
`combineLatest()` on `Collection` where elements are Publishers
View CombineLatestCollection.swift
import Combine
import Foundation
extension Collection where Element: Publisher {
public func combineLatest() -> CombineLatestCollection<Self> {
CombineLatestCollection(self)
}
}
@ollieatkinson
ollieatkinson / Decodable+ContainerType.swift
Last active Jun 19, 2021
Encoding / Decoding Container Types
View Decodable+ContainerType.swift
enum DecodingContainer: String {
case keyed
case unkeyed
case singleValue
}
extension Decodable {
static var container: DecodingContainer {
do {
@ollieatkinson
ollieatkinson / AttributesOf.swift
Last active May 22, 2021
Composition of attributes and values for types in Swift
View AttributesOf.swift
struct AttributesOf<Object>: CustomStringConvertible {
enum Error: Swift.Error { case message(String) }
public typealias Assignment = (keyPath: AnyKeyPath, set: (Object) -> Void)
private(set) var assignments: [Assignment]
init(@AttributesOfBuilder<Object> _ builder: () throws -> [Assignment]) rethrows {
assignments = try builder()
@ollieatkinson
ollieatkinson / Dictionary+DeepMerge.swift
Last active May 21, 2021
DeepMerge two Dictionary's in Swift
View Dictionary+DeepMerge.swift
public enum DeepMergeUniquingPolicy {
case old, new
}
extension Dictionary {
public func deepMerging(_ other: Self, uniquingPolicy policy: DeepMergeUniquingPolicy = .new) -> Self {
(self as [Key: Any]).deepMerging(other as [Key: Any], uniquingPolicy: policy) as! Self
}
}
View String+Error.swift
extension Optional {
public func or(throw error: @autoclosure () -> Error) throws -> Wrapped {
guard let wrapped = self else { throw error() }
return wrapped
}
}
extension String {
@ollieatkinson
ollieatkinson / SVG.swift
Last active May 7, 2022
Utilise the private CoreSVG framework in Swift
View SVG.swift
import Darwin
import Foundation
import UIKit
@objc
class CGSVGDocument: NSObject { }
var CGSVGDocumentRetain: (@convention(c) (CGSVGDocument?) -> Unmanaged<CGSVGDocument>?) = load("CGSVGDocumentRetain")
var CGSVGDocumentRelease: (@convention(c) (CGSVGDocument?) -> Void) = load("CGSVGDocumentRelease")
var CGSVGDocumentCreateFromData: (@convention(c) (CFData?, CFDictionary?) -> Unmanaged<CGSVGDocument>?) = load("CGSVGDocumentCreateFromData")