Skip to content

Instantly share code, notes, and snippets.

Avatar

David Beck davbeck

View GitHub Profile
View TextScaling.swift
struct ContentView: View {
var text: Text {
Text("Bacon ipsum dolor amet fatback rump beef ribs jerky pork loin strip steak.")
.font(.headline) +
Text("\nCorned beef t-bone leberkas ball tip tongue burgdoggen picanha swine porchetta flank hamburger strip steak tail pork. Filet mignon prosciutto venison tongue meatball shankle pancetta. Hamburger prosciutto turkey chicken venison tenderloin porchetta spare ribs burgdoggen cupim pork turducken. Short ribs andouille kielbasa short loin beef. Ham kevin pork loin bacon, pastrami turducken jowl pig venison pork shank beef picanha.")
.font(.body)
}
var body: some View {
text
@davbeck
davbeck / Example.swift
Created Jul 31, 2020
Callback api for SwiftUI based on React's useEffect hook
View Example.swift
struct Child: View {
var count: Int
var body: some View {
Text("Child: \(count)")
.useEffect(Int(floor(Double(count) / 2))) { source in
// this only gets called when the source changes
print("onAppear \(source)")
}
}
View ObservablePublisher.swift
import Combine
import Foundation
import SwiftUI
class ObservablePublisher<Output>: ObservableObject {
@Published var output: Output?
@Published var error: Swift.Error?
private var observer: AnyCancellable?
init() {}
View IgnoreHashable.swift
@propertyWrapper
struct IgnoreHashable<T>: Hashable {
var wrappedValue: T
init(wrappedValue: T) {
self.wrappedValue = wrappedValue
}
func hash(into hasher: inout Hasher) {}
@davbeck
davbeck / Loader.swift
Created Jul 21, 2020
Loader for SwiftUI derived data
View Loader.swift
class Loader<Output>: ObservableObject {
@Published var isLoding: Bool = false
@Published var error: Swift.Error?
@Published var value: Output?
private var observer: AnyCancellable?
func load<P: Publisher>(_ publisher: P) where P.Output == Output {
self.observer?.cancel()
View FixHiddenCharacters.sh
# remap windows menu/application key to option to avoid hidden characters
# in particular when using the Microsoft ergonomic keyboard
# I found that I would accidentally insert hidden characters when pasting
hidutil property --set '{"UserKeyMapping":
[{"HIDKeyboardModifierMappingSrc":0x700000065,
"HIDKeyboardModifierMappingDst":0x7000000e6}]
}'
View Lock.swift
struct Lock {
private var unfairLock = os_unfair_lock_s()
mutating func lock(_ work: () -> Void) {
self.lock()
work()
self.unlock()
}
mutating func trylock(_ work: () -> Void) -> Bool {
@davbeck
davbeck / AssociatedProperty.swift
Created Feb 10, 2020
Swift friendly wrapper around associated objects.
View AssociatedProperty.swift
import ObjectiveC
extension objc_AssociationPolicy {
public static let retain = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN
public static let retainNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
public static let copy = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY
public static let copyNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC
public static let assign = objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN
}
View AllowDecodeFailure.swift
import Foundation
/// Allows a property to fail decoding, defaulting to nil instead of surfacing the error.
@propertyWrapper
struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable {
var wrappedValue: Wrapped?
init() {
self.wrappedValue = nil
}
@davbeck
davbeck / IfElseTernary.swift
Created Oct 22, 2019
Use if/else logic instead of a ternary operator in Swift. Don't do this.
View IfElseTernary.swift
func _if<T>(_ test: Bool, action: () -> T) -> T? {
if test {
return action()
} else {
return nil
}
}
extension Optional {
func _else(action: () -> Wrapped) -> Wrapped {
You can’t perform that action at this time.