Skip to content

Instantly share code, notes, and snippets.

Avatar

Jérôme Alves jegnux

View GitHub Profile
View iOS13_onChange.swift
import SwiftUI
import Combine
public struct ChangeObserver<V: Equatable>: ViewModifier {
public init(newValue: V, action: @escaping (V) -> Void) {
self.newValue = newValue
self.newAction = action
}
private typealias Action = (V) -> Void
View Result+Catch.swift
extension Result {
public func `catch`(_ handler: () throws -> Success) -> Result<Success, Error> {
flatMapError { _ in
.init { try handler() }
}
}
public func `catch`(_ handler: (Failure) throws -> Success) -> Result<Success, Error> {
flatMapError { error in
.init { try handler(error) }
@jegnux
jegnux / SuperPublished.swift
Last active Oct 12, 2021
Alternative to @published that works with subclasses
View SuperPublished.swift
@propertyWrapper
public struct SuperPublished<Value> {
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
public var wrappedValue: Value
public static subscript<EnclosingSelf: ObservableObject>(
View CascadeOperator.swift
prefix operator ..
infix operator ..: MultiplicationPrecedence
/*
Custom operator that lets you configure an instance inline
```swift
let label = UILabel()..{
$0.numberOfLines = 0
$0.textColor = .systemRed
}
View ProxyPropertyWrapper.swift
@propertyWrapper
public struct AnyProxy<EnclosingSelf, Value> {
private let keyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>
public init(_ keyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>) {
self.keyPath = keyPath
}
@available(*, unavailable, message: "The wrapped value must be accessed from the enclosing instance property.")
public var wrappedValue: Value {