This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lazy<T> { | |
private(set) var created: Bool = false | |
private(set) lazy var value: T = { | |
created = true | |
return builder() | |
}() | |
private var builder: () -> T | |
init(_ builder: @escaping () -> T) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AnyPublisher where Failure: Error { | |
struct Subscriber { | |
fileprivate let send: (Output) -> Void | |
fileprivate let complete: (Subscribers.Completion<Failure>) -> Void | |
func send(_ value: Output) { self.send(value) } | |
func send(completion: Subscribers.Completion<Failure>) { self.complete(completion) } | |
} | |
init(_ closure: (Subscriber) -> AnyCancellable) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
extension EnvironmentValues { | |
public func value<T>(_: T.Type = T.self, forKey key: String) -> T? { | |
guard let value = first(where: { name($0, equals: key) }) else { | |
print("No EnvironmentValue with key '\(key)' found.") | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public protocol CodingStrategy { | |
associatedtype Converted | |
associatedtype RawValue: Codable | |
static func toRaw(_ value: Converted) throws -> RawValue | |
static func toValue(_ raw: RawValue) throws -> Converted | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A wrapper for a partial representation of a `T`. | |
/// It can be constructed over time, then later used to | |
/// build a complete `T` | |
@dynamicMemberLookup | |
public struct Partial<T> { | |
enum Error: Swift.Error { | |
case invalid(PartialKeyPath<T>) | |
} | |
// MARK: - Private Properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Foo: Codable { | |
var name: String | |
@Ignore var foo: Int? | |
} | |
let model = Foo(name: "Ian", foo: 42) | |
let data = try! JSONEncoder().encode(model) | |
print(String(data: data, encoding: .utf8)!) // {"name":"Ian"} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
let isUITesting = /* your UI test detection here */ | |
@main | |
struct EntryPoint { | |
static func main() { | |
if isUITesting { | |
UITestApp.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct AnyCodable: Codable { | |
public var base: Any | |
public init(_ base: Any) { | |
self.base = base | |
} | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
guard !container.decodeNil() else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup | |
class Child: ObservableObject { | |
@Published var value = "" | |
} | |
class Parent: ObservableObjectContainer { | |
let single = Child() | |
let array = [Child(), Child()] | |
func updateSingle() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Validator where Input == User, Output == User { | |
static var validUser: Validator<User, User> { | |
return .keyPath(\.name, .isNotNil && .isNotEmpty) | |
} | |
} | |
struct User { | |
let name: String? | |
} |