Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀

Casper Zandbergen Amzd

🚀
View GitHub Profile
@Amzd
Amzd / EmptyOrNil.swift
Created Nov 12, 2021 — forked from casperzandbergenyaacomm/EmptyOrNil.swift
Adding readability for an if statement I often use.
View EmptyOrNil.swift
protocol EmptyOrNil {
var isEmpty: Bool { get }
}
extension Optional where Wrapped: EmptyOrNil {
var isEmptyOrNil: Bool {
return self?.isEmpty ?? true
}
}
@Amzd
Amzd / DictionaryKeyPath.swift
Last active Jul 12, 2021 — forked from casperzandbergenyaacomm/DictionaryKeyPath.swift
Reading and writing to (possible) nested dictionaries for a given key path, using a recursive approach
View DictionaryKeyPath.swift
// Inspired by: https://gist.github.com/dfrib/d7419038f7e680d3f268750d63f0dfae
import Foundation
public extension Dictionary {
subscript(keyPath string: Key, separator: String) -> Value? where Key == String {
get { return self[keyPath: string.components(separatedBy: separator)] }
set { self[keyPath: string.components(separatedBy: separator)] = newValue }
}
@Amzd
Amzd / StateObject.swift
Last active Nov 4, 2021
StateObject that works in iOS 13
View StateObject.swift
import Combine
import PublishedObject // https://github.com/Amzd/PublishedObject
/// A property wrapper type that instantiates an observable object.
@propertyWrapper
public struct StateObject<ObjectType: ObservableObject>: DynamicProperty
where ObjectType.ObjectWillChangePublisher == ObservableObjectPublisher {
/// Wrapper that helps with initialising without actually having an ObservableObject yet
private class ObservedObjectWrapper: ObservableObject {
View DateRounding.swift
import Foundation
extension Date {
/// Returns date where **-component** is rounded to its closest
/// multiple of **-amount**. Warning: month and day start at 1
/// so round(to: 6, .month) will either return month 1 or 7!
func round(to amount: Int, _ component: Calendar.Component) -> Date {
let cal = Calendar.current
var value = cal.component(component, from: self)
@Amzd
Amzd / AppendingClosures.swift
Last active Jun 14, 2021
Appending Closures, eg a shorthand to create one new closure that calls two closures.
View AppendingClosures.swift
// MARK: Appending closures `a + b`
/// Append closures, lhs first.
public func + <I>(lhs: @escaping (I) -> Void, rhs: @escaping (I) -> Void) -> ((I) -> Void) {
return {
lhs($0)
rhs($0)
}
}
@Amzd
Amzd / ColorPicker.swift
Last active Feb 20, 2021
What SwiftUI's ColorPicker should have been.
View ColorPicker.swift
import SwiftUI
@available(iOS 14.0, *)
public struct ColorPickerWithoutLabel: UIViewRepresentable {
@Binding var selection: Color
var supportsAlpha: Bool = true
public init(selection: Binding<Color>, supportsAlpha: Bool = true) {
self._selection = selection
self.supportsAlpha = supportsAlpha
@Amzd
Amzd / Publisher+void.swift
Last active Jan 16, 2021
Void publisher result
View Publisher+void.swift
extension Publisher {
func void() -> Publishers.Map<Self, Void> {
self.map { _ in () }
}
}
@Amzd
Amzd / AnyPublisher.swift
Created Dec 7, 2020
Empty and constant AnyPublisher extensions
View AnyPublisher.swift
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension AnyPublisher {
static var empty: Self {
Empty(completeImmediately: false).eraseToAnyPublisher()
}
static func constant(_ value: Output) -> Self where Failure == Never {
Just(value).eraseToAnyPublisher()
}
}
@Amzd
Amzd / HierarchyDescription.swift
Last active Nov 30, 2020
Description that can be used with any type that has recursive children
View HierarchyDescription.swift
public protocol HierarchyDescription {
associatedtype Child: HierarchyDescription
var children: [Child] { get }
var description: String { get }
}
extension HierarchyDescription {
public var hierarchyDescription: String {
var description = self.description
for child in children {
@Amzd
Amzd / View+NSCursor.swift
Last active Apr 18, 2021
Set the cursor that is displayed when hovering a View. (macOS, SwiftUI)
View View+NSCursor.swift
import SwiftUI
extension View {
/// https://stackoverflow.com/a/61985678/3393964
public func cursor(_ cursor: NSCursor) -> some View {
self.onHover { inside in
if inside {
cursor.push()
} else {
NSCursor.pop()