Skip to content

Instantly share code, notes, and snippets.

View jegnux's full-sized avatar

Jérôme Alves jegnux

View GitHub Profile
@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 {
prefix operator ..
infix operator ..: MultiplicationPrecedence
/*
Custom operator that lets you configure an instance inline
```swift
let label = UILabel()..{
$0.numberOfLines = 0
$0.textColor = .systemRed
}
@jegnux
jegnux / SuperPublished.swift
Last active October 12, 2021 22:49
Alternative to @published that works with subclasses
@propertyWrapper
public struct SuperPublished<Value> {
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
public var wrappedValue: Value
public static subscript<EnclosingSelf: ObservableObject>(
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) }
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
import Foundation
import SwiftUI
public struct DependencyBag<Dependency, Content: View> : View {
public init(
dependencyFactory: @escaping () -> Dependency,
@ViewBuilder view contentFactory: @escaping (Dependency) -> Content
) {
self.dependencyFactory = dependencyFactory
self.contentFactory = contentFactory