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 SwiftUI | |
extension View { | |
public func discover<T: UIView>( | |
tag: Int = .random(in: (.min)...(.max)), | |
where predicate: @escaping (T) -> Bool = { _ in true }, | |
_ closure: @escaping (T) -> Void | |
) -> some View { | |
self.overlay( | |
DiscoveryView(tag: tag) |
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
@propertyWrapper | |
struct LosslessKey<T: Hashable & LosslessStringConvertible, U> { | |
var wrappedValue: [T: U] | |
} | |
extension LosslessKey: Codable where T: Codable, U: Codable { | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
var result: [T: U] = [:] | |
for (key, value) in try container.decode([String: U].self) { | |
guard let key = T.init(key) 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
public protocol CustomEquatable: Equatable { } | |
private extension CustomEquatable { | |
var equatables: [AnyEquatable] { | |
return Mirror(reflecting: self) | |
.children | |
.compactMap { ($0.value as? EquatingProperty)?.anyEquatable } | |
} | |
} |
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 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 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 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 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
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"} | |