Skip to content

Instantly share code, notes, and snippets.

View AlexChekel1337's full-sized avatar

Alexander Chekel AlexChekel1337

View GitHub Profile
@AlexChekel1337
AlexChekel1337 / ShadowCutoutView.swift
Created August 21, 2024 18:56
A view that combines shadow, corner radius, and translucent background into one view
class ShadowCutoutView: UIView {
var contentView: UIView? {
didSet {
oldValue?.removeFromSuperview()
guard let contentView else {
return
}
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
@AlexChekel1337
AlexChekel1337 / ViewLoader.swift
Created June 11, 2024 14:56
A backport of @ViewLoading property wrapper from iOS 16.4
extension UIViewController {
/// A property wrapper that loads the view controller’s view before accessing the property.
/// A backport of `@ViewLoading` property wrapper, available in iOS 16.4.
/// https://developer.apple.com/documentation/uikit/uiviewcontroller/viewloading
@propertyWrapper
struct ViewLoader<Value> {
static subscript<T: UIViewController>(
_enclosingInstance instance: T,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<T, Self>
@AlexChekel1337
AlexChekel1337 / UIView+LabeledEdges.swift
Last active December 8, 2022 12:53
Labels edges of a given view
extension UIView {
struct LabeledEdge: OptionSet {
let rawValue: Int
static let leading = Self(rawValue: 1 << 0)
static let top = Self(rawValue: 1 << 1)
static let trailing = Self(rawValue: 1 << 2)
static let bottom = Self(rawValue: 1 << 3)
static let all: Self = [.leading, .top, .trailing, .bottom]
@AlexChekel1337
AlexChekel1337 / XCTAssertSuccess.swift
Created February 24, 2022 20:34
Asserts that given result is a success, and if so, returns a success value
func XCTAssertSuccess<T, U>(
_ result: @autoclosure () throws -> Result<T, U>,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) -> T {
do {
let value = try result()
switch value {
@AlexChekel1337
AlexChekel1337 / Result+Void.swift
Created February 16, 2022 15:00
Swift Result
extension Result where Success == Void {
static var success: Result<Success, Failure> {
return .success(())
}
}
// Example usage
func someFunctionThatReturnsResult() -> Result<Void, Error> {
// Allows to use just `.success` instead of `.success(())`