Skip to content

Instantly share code, notes, and snippets.

View ollieatkinson's full-sized avatar

Oliver Atkinson ollieatkinson

View GitHub Profile
@ollieatkinson
ollieatkinson / SVG.swift
Last active May 9, 2024 19:39
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 / Tuple.swift
Last active April 22, 2024 15:13
Implementation of `Tuple` type using Swift's new parameter packs
public struct Tuple<each T> {
public private(set) var value: (repeat each T)
public init(_ value: repeat each T) { self.value = (repeat each value) }
}
extension Tuple {
public func map<each U>(
_ transform: (repeat each T) throws -> (repeat each U)
) rethrows -> (repeat each U) {
@ollieatkinson
ollieatkinson / HTTPStatusCode.swift
Last active April 15, 2024 18:34
HTTP status codes as a Swift enum.
/// This is a list of Hypertext Transfer Protocol (HTTP) response status codes.
/// It includes codes from IETF internet standards, other IETF RFCs, other specifications, and some additional commonly used codes.
/// The first digit of the status code specifies one of five classes of response; an HTTP client must recognise these five classes at a minimum.
enum HTTPStatusCode: Int, Error {
/// The response class representation of status codes, these get grouped by their first digit.
enum ResponseType {
/// - informational: This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line.
case informational
@ollieatkinson
ollieatkinson / JSONObject.swift
Last active April 8, 2024 21:41
JSON Equatable in Swift
public let null = NSNull() as AnyHashable
public enum JSONObject {
case array([Any])
case dictionary([String: Any])
case fragment(Any)
}
extension JSONObject {
@inlinable public static func with(_ data: Data, options: JSONSerialization.ReadingOptions = []) throws -> JSONObject {
@ollieatkinson
ollieatkinson / AnyEquatable.swift
Last active February 20, 2024 13:28
Equatable `Any` using existential
public func isEqual(_ x: Any, _ y: Any) -> Bool {
if let isEqual = (x as? any Equatable)?.isEqual(to: y) {
return isEqual
} else if let equatable = x as? AnyEquatable {
return equatable.isEqual(to: y)
} else {
return (x as? any OptionalProtocol).isNil && (y as? any OptionalProtocol).isNil
}
}
@ollieatkinson
ollieatkinson / CGContextRepresentable.swift
Created February 20, 2024 09:19
Render to a CGContext directly from SwiftUI
protocol CGContextRepresentable: View {
func draw(in context: CGContext, frame: CGRect)
}
extension CGContextRepresentable {
var body: some View { _CGContextView(draw: draw) }
}
private struct _CGContextView: UIViewRepresentable {
let draw: (CGContext, CGRect) -> Void
@ollieatkinson
ollieatkinson / OnChangeObservable.swift
Created February 7, 2024 10:40
OnChangeObservable property wrapper
@propertyWrapper
public struct OnChangeObservable<Value: Equatable> {
private var storage: Storage
public var wrappedValue: Value {
get { storage.value }
set { storage.value = newValue }
}
public var projectedValue: OnChangeObservable<Value> {
@ollieatkinson
ollieatkinson / FileSystemEventStream.swift
Created October 22, 2020 10:27
sane `fsevents` in Swift
import Foundation
import Combine
public struct Event: Identifiable {
public let id: FSEventStreamEventId
public let path: String
public let flags: Flags
}
public class FileSystemEventStream {
@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 / Dictionary+KeyPath.swift
Last active January 7, 2024 03:30
KeyPath to get/set from a Swift JSON object ([String: Any] or [Any])
import Foundation
extension Dictionary where Key == String, Value == Any {
public subscript(keyPath: JSON.Path.Index...) -> Value? {
get { self[keyPath: .init(path: keyPath)] }
set { self[keyPath: .init(path: keyPath)] = newValue }
}
public subscript(keyPath keyPath: JSON.Path) -> Value? {