Skip to content

Instantly share code, notes, and snippets.

View ollieatkinson's full-sized avatar

Oliver Atkinson ollieatkinson

View GitHub Profile
@ollieatkinson
ollieatkinson / AttributesOf.swift
Last active March 10, 2023 17:41
Composition of attributes and values for types in 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 July 31, 2022 19:29
DeepMerge and DeepMerge for Dictionary's in Swift
public struct DeepMapOptions: OptionSet {
public let rawValue: UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let mappingOverArrays = DeepMapOptions(rawValue: 1 << 0)
public static let all: DeepMapOptions = [.mappingOverArrays]
}
extension Dictionary where Value == Any {
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 February 23, 2024 04:59
Utilise the private CoreSVG framework in Swift
import Darwin
import Foundation
import UIKit
// https://github.com/xybp888/iOS-SDKs/blob/master/iPhoneOS17.1.sdk/System/Library/PrivateFrameworks/CoreSVG.framework/CoreSVG.tbd
// https://developer.limneos.net/index.php?ios=17.1&framework=UIKitCore.framework&header=UIImage.h
@objc
class CGSVGDocument: NSObject { }
@ollieatkinson
ollieatkinson / Publisher.swift
Created April 29, 2021 11:06
Useful extensions for Combine Publisher
extension Publisher where Failure == Never {
public func sink<Root>(to handler: @escaping (Root) -> (Output) -> Void, on root: Root) -> AnyCancellable where Root: AnyObject {
sink{ [weak root] value in
guard let root = root else { return }
handler(root)(value)
}
}
@ollieatkinson
ollieatkinson / UIGestureRecognizer.Publisher.swift
Created April 29, 2021 10:48
Combine publisher for UIGestureRecognizer
extension UIView {
func publisher<G>(for gestureRecognizer: G) -> UIGestureRecognizer.Publisher<G> where G: UIGestureRecognizer {
UIGestureRecognizer.Publisher(gestureRecognizer: gestureRecognizer, view: self)
}
}
extension UIGestureRecognizer {
struct Publisher<G>: Combine.Publisher where G: UIGestureRecognizer {
@ollieatkinson
ollieatkinson / JSON.swift
Created March 15, 2021 09:42
Another JSON box implementation over `Any` type
struct JSON {
enum Index {
case ordinal(Int), key(String)
}
private(set) var any: Any?
init(_ any: Any? = nil) {
self.any = any
@ollieatkinson
ollieatkinson / KeyValuePairs.swift
Created March 8, 2021 09:16
Key value pairing with support for equality, hashing and coding
extension Dictionary {
public typealias KeyValuePairs = [KeyValuePair<Key, Value>]
public var keyValuePairs: KeyValuePairs { map(KeyValuePair.init) }
}
extension Collection where Element: KeyValuePair_P {
public var dictionary: [Element.Key: Element.Value] { Dictionary(uniqueKeysWithValues: map(\.tuple)) }
}
public protocol KeyValuePair_P {
@ollieatkinson
ollieatkinson / AnyDictionary.swift
Last active December 26, 2020 20:12
AnyDictionary
protocol AnyDictionary: Collection {
associatedtype Key: Hashable
associatedtype Keys
associatedtype Value
associatedtype Values
associatedtype Index
var keys: Keys { get }
var values: Values { get }
@ollieatkinson
ollieatkinson / AtomicCollection.swift
Created December 26, 2020 18:00
Atomic read/write access for collections
import Foundation
class AtomicCollection<C> where C: Collection {
var base: C
let queue: DispatchQueue
init(_ base: C, label: String, qos: DispatchQoS = .unspecified, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = .inherit, target: DispatchQueue? = nil) {
self.base = base
self.queue = DispatchQueue(