Skip to content

Instantly share code, notes, and snippets.

View Noobish1's full-sized avatar

Blair McArthur Noobish1

View GitHub Profile
@ole
ole / swift-has-feature.sh
Last active June 21, 2024 14:32
swift-list-feature: List Swift compiler upcoming and experimental feature flags. ★ swift-has-feature: Check if a given compiler knows a specific feature flag, and whether it's an upcoming or experimental flag.
#!/bin/zsh
# Test if the Swift compiler knows about a particular language feature.
#
# Usage:
#
# swift-has-feature [--swift SWIFT_PATH] [--language-version LANGUAGE_VERSION] FEATURE
#
# The feature should be an upcoming or experimental language feature,
# such as `"StrictConcurrency"` or `"ExistentialAny"`.
//
// Created by Sean Heber on 8/11/22.
//
import Foundation
enum ExponentialBackoffError : Error {
case retryLimitExceeded
}
//
// View+Assign.swift
// Created by Sean on 7/29/22.
//
import SwiftUI
// You cannot safely assign to a state variable during view update - such as inside the block of a GeometryReader.
// Rather than do an unsafe hack like DispatchQueue.main.async or resorting to a PreferenceKey or even Combine, we
// can simply defer the assignment to a time when it *is* safe to update State- such as inside of the task block!
@swiftui-lab
swiftui-lab / grid-trainer.swift
Last active March 29, 2024 01:46
A grid trainer for Grid views
// Author: SwiftUI-Lab (swiftui-lab.com)
// Description: this learning tool is designed to showcase the different
// Grid and GridRow view options, added in SwiftUI 2022. It is part of the
// blog article: https://swiftui-lab.com/eager-grids
//
import SwiftUI
import UniformTypeIdentifiers
// The root view of the application
struct ContentView: View {
@joshdholtz
joshdholtz / ATinySampleApp.swift
Last active March 29, 2024 23:27
Super basic SwiftUI app (70 lines of code) with paywall using RevenueCat
import SwiftUI
import RevenueCat
struct Constants {
static let apiKey = "<your_api_key>" // Will look like: appl_bunchofotherstuffhere
static let entitlementName = "<your_entitlement_name>" // I use something like "pro"
}
@main
struct ATinySampleApp: App {
@ryanlintott
ryanlintott / LayoutThatFits.swift
Last active December 8, 2023 15:14
An alternative to ViewThatFits. Updated version can be found here: https://github.com/ryanlintott/LayoutThatFits
//
// LayoutThatFits.swift
// WWDC22Experiments
//
// Created by Ryan Lintott on 2022-06-08.
//
import SwiftUI
struct LayoutThatFits: Layout {
import Nimble
import Quick
/// Replacement for Quick's `it` which runs using swift concurrency.
func asyncIt(
_ description: String,
file: StaticString = #file,
line: UInt = #line,
closure: @MainActor @escaping () async throws -> Void
) {
import SwiftUI
import Combine
public struct ChangeObserver<V: Equatable>: ViewModifier {
public init(newValue: V, action: @escaping (V) -> Void) {
self.newValue = newValue
self.newAction = action
}
private typealias Action = (V) -> Void
@douglashill
douglashill / LazyLet.swift
Created April 22, 2020 08:04
A Swift property wrapper that implements ‘lazy let’. I.e. a read-only property that loads its value when first read. Not safe for access from multiple threads.
/// A Swift property wrapper that implements ‘lazy let’. I.e. a read-only property that loads its value when first read.
/// Not safe for access from multiple threads.
/// Does not work the same as the lazy keyboard because the property initialiser will run before self is available.
/// Adapted from https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md
@propertyWrapper enum LazyLet<Value> {
case uninitialised(() -> Value)
case initialised(Value)
init(wrappedValue: @autoclosure @escaping () -> Value) {
self = .uninitialised(wrappedValue)
@alexpersian
alexpersian / UITextView+Truncated.swift
Last active June 21, 2024 10:03
Extension on UITextView that allows for the detection of truncation.
extension UITextView {
// Massive credit to Dave Delong for his extensive help with this solution.
/// Returns whether or not the `UITextView` is displaying truncated text. This includes text
/// that is visually truncated with an ellipsis (...), and text that is simply cut off through
/// word wrapping.
///
/// - Important:
/// This only works properly when the `NSLineBreakMode` is set to `.byTruncatingTail` or `.byWordWrapping`.
///
/// - Remark: