Skip to content

Instantly share code, notes, and snippets.

View helje5's full-sized avatar

Helge Heß helje5

View GitHub Profile
@helje5
helje5 / ListCellSizingFlicker.swift
Last active October 16, 2023 21:30
ListCellSizingFlicker
import SwiftUI
struct Item: Identifiable {
let id = UUID()
let title = "Hello"
}
let items = [
Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(),
Item(), Item(), Item(), Item(), Item(), Item(), Item(),
@helje5
helje5 / StateObservable.swift
Created September 16, 2023 13:16
An `Observable` property wrapper that only creates the observable value once
/**
* An Observable property wrapper that only creates the observable
* value once.
*
* Example:
* ```swift
* @Observable class Item {
* }
*
* struct MyView: View {
@helje5
helje5 / RepeatedModelInits.swift
Last active September 16, 2023 11:34
Repeated Model Inits w/ State
import SwiftUI
@Observable class Tester {
init() { print("INIT:", ObjectIdentifier(self)) }
deinit { print("DEINIT:", ObjectIdentifier(self)) }
}
struct ContentView: View {
struct Nested: View {
@helje5
helje5 / SwiftData Inverse Relationships.swift
Created September 13, 2023 21:36
SwiftData Inverse Relationships
import SwiftUI
import SwiftData
@Model final class Address {
let street = "Am Geldspeicher 1"
var contact : Contact?
init() {}
}
@Model final class Contact {
@helje5
helje5 / SwiftUIDataUnitTest.swift
Created August 17, 2023 16:19
Unit Testing a SwiftUI Query
//
// Created by Helge Heß.
// Copyright © 2023 ZeeZide GmbH.
//
import XCTest
import UIKit
import SwiftData
import SwiftUI
@helje5
helje5 / CompareAnySwift57.swift
Created August 2, 2023 16:20
Compare Any w/ Swift 5.7
func isEqual(_ lhs: Any, _ rhs: Any) -> Bool {
guard let lhs = lhs as? any Equatable else { return false }
func isEqual<T: Equatable>(lhs: T, rhs: Any) -> Bool {
guard let rhs = rhs as? T else { return false }
return lhs == rhs
}
return isEqual(lhs: lhs, rhs: rhs)
}
@helje5
helje5 / cal.swift
Created February 20, 2022 15:13
A completely incomplete implementation of `cal` using Swift / Foundation.Calendar
#!/usr/bin/swift
import Foundation
extension DateInterval {
func containsOpen(_ date: Date) -> Bool { date >= start && date < end }
}
struct MonthCalendar {
@helje5
helje5 / Tows.swift
Last active June 12, 2022 06:33
An 82-liner SwiftUI script similar to CodeCows 🐮
import SwiftUI
import cows // @AlwaysRightInstitute
struct ContentView: View {
@State var searchString = ""
@State var matches = allCows
@State var selectedCow : String?
let font = Font(NSFont
@helje5
helje5 / servedocc.swift
Last active July 16, 2022 22:24
Small Swift Script to serve `.doccarchive`s to the browser
#!/usr/bin/swift sh
import MacroExpress // @Macro-swift
// MARK: - Parse Commandline Arguments & Usage
func usage() {
let tool = path.basename(process.argv.first ?? "servedocc")
print(
"""
@helje5
helje5 / main.swift
Last active January 12, 2022 18:44
Using async/await concurrency on iOS 14 and before
// Created by Helge Heß 2021-06-17
import Foundation
// They use obfuscated names to hide it from us!
import JavaScriptCore
/// Setup our async/await runtime.
let runtime = JSContext()!