Skip to content

Instantly share code, notes, and snippets.

@DavidBemerguy
DavidBemerguy / RoundingPrecision.swift
Created July 17, 2023 10:38
Specify the decimal place to round to using an enum
// Specify the decimal place to round to using an enum
public enum RoundingPrecision {
case ones
case tenths
case hundredths
}
// Round to the specific decimal place
public func preciseRound(
_ value: Double,
@DavidBemerguy
DavidBemerguy / ContentView.swift
Created June 18, 2023 09:34
View modifier for dynamic color
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.dynamicColor((.blue, .red))
}
.padding()
}
import Network
class PathMonitor: ObservableObject {
@Published private(set) var status: NWPath.Status
private let pathMonitor = NWPathMonitor()
private let pathMonitorQueue = DispatchQueue(label: "NWPathMonitor")
init(status: NWPath.Status = .unsatisfied, active: Bool = true) {
self.status = status
@DavidBemerguy
DavidBemerguy / IgnoreEquatableAndHashable.swift
Created May 4, 2023 14:51
PropertyWrapper that forces the compiler to ignore non hashable or non equatable properties
@propertyWrapper
public struct IgnoreEquatable<Wrapped>: Equatable {
public var wrappedValue: Wrapped
public static func == (
lhs: IgnoreEquatable<Wrapped>,
rhs: IgnoreEquatable<Wrapped>
) -> Bool {
true
}
@DavidBemerguy
DavidBemerguy / Versioned.swift
Created April 27, 2023 05:58
Property wrapper that keeps versioning of the property - Very useful for debugging
@propertyWrapper
struct Versioned<Value> {
private var currentValue: Value
private(set) var history: [Value] = []
init(wrappedValue: Value) {
self.currentValue = wrappedValue
}
var wrappedValue: Value {
@DavidBemerguy
DavidBemerguy / NullableContinuation.swift
Created December 18, 2022 08:39
Swift NullableContinuation
import Foundation
/// API's out of our control can sometimes callback more than once, causing the continuation to crash
/// This class is a wrapper that should avoid this crash
/// Not recommended to be used largely, but it can help if really needed to use the problematic API
public class NullableContinuation<T, E> where E : Error {
private var checkedContinuation: CheckedContinuation<T,E>?
public init(checkedContinuation: CheckedContinuation<T,E>) {