This file contains hidden or 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 View { | |
func relative(width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) -> some View { | |
Color.clear | |
.frame(maxWidth: width.map { _ in .infinity }, maxHeight: height.map { _ in .infinity }) | |
.overlay(GeometryReader { proxy in | |
ZStack { | |
self.frame( | |
width: width.map { proxy.size.width * $0 }, | |
height: height.map { proxy.size.height * $0 } | |
) |
This file contains hidden or 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
func sync<T, U>(file: StaticString = #file, line: UInt = #line, timeOut: TimeInterval = 3, _ function: @escaping (@escaping (T) -> Void) -> U) -> T { | |
let group = DispatchGroup() | |
group.enter() | |
var result: T! | |
DispatchQueue.global().async { | |
_ = function { | |
result = $0 | |
group.leave() | |
} |
This file contains hidden or 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 | |
@propertyWrapper | |
struct Coding<T: NSObject & NSSecureCoding>: Codable { | |
var wrappedValue: T | |
init(from decoder: Decoder) throws { | |
let string = try String(from: decoder) | |
guard let value = try NSKeyedUnarchiver.unarchivedObject(ofClass: T.self, from: Data(string.utf8)) else { | |
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "")) |
This file contains hidden or 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: Sendable { | |
fileprivate let send: @Sendable (Output) -> Void | |
fileprivate let complete: @Sendable (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 hidden or 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 hidden or 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
let source = PassthroughSubject<Int, Error>() | |
let other = PassthroughSubject<String, Error>() | |
let subscription = source.withLatestFrom(other).assertNoFailure().sink(receiveValue: { value in | |
print(value) | |
}) | |
source.send(1) | |
other.send("hello") | |
source.send(2) |
This file contains hidden or 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
@propertyWrapper | |
struct AppStorage<Value>: DynamicProperty { | |
let key: String | |
let defaultValue: Value | |
init(wrappedValue defaultValue: Value, _ key: String) { | |
self.key = key | |
self.defaultValue = defaultValue | |
self._wrappedValue = State(wrappedValue: UserDefaults.standard.object(forKey: key) as? Value ?? defaultValue) | |
} |
This file contains hidden or 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 DictionaryKeyPathError: Error, CustomStringConvertible { | |
public enum FragmentError: Error { | |
case valueMissing | |
case valueMismatch(expected: Any.Type, received: Any.Type, value: Any) | |
} | |
public let path: [Any] | |
public let error: FragmentError | |
public var description: String { |