Skip to content

Instantly share code, notes, and snippets.

View harlanhaskins's full-sized avatar
🦅
Swifting

Harlan Haskins harlanhaskins

🦅
Swifting
View GitHub Profile
@harlanhaskins
harlanhaskins / View+ParentViewController.swift
Last active March 5, 2025 23:06
A SwiftUI modifier for inserting the parent UIViewController in the environment
import Foundation
import SwiftUI
extension View {
/// Adds introspection to find the parent view controller in the view hierarchy and
/// makes that view controller available to downstream views in the view hierarchy.
public func addParentViewControllerIntrospection() -> some View {
modifier(ParentViewControllerEnvironmentModifier())
}
}
@harlanhaskins
harlanhaskins / UIKitDefaults.swift
Created December 20, 2024 19:27
UIKit user defaults for gesture logging
@MainActor
enum UIKitDefaults {
static let defaults = UserDefaults(suiteName: "com.apple.UIKit")!
// Enables UIKit gesture logging, which will log the set of gesture recognizers responding to a touch and their states.
static var gestureLoggingEnabled: Bool {
get { defaults.bool(forKey: "LogGesture") }
set { defaults.set(newValue, forKey: "LogGesture") }
}
@harlanhaskins
harlanhaskins / AssociatedObjects.swift
Created November 23, 2024 16:26
A nice little type-safe wrapper around ObjC associated objects
//
// AssociatedObjects.swift
//
// Created by Harlan Haskins on 11/22/24.
//
import Foundation
internal import ObjectiveC
/// The ownership semantics by which an Objective-C associated object will be stored.
@harlanhaskins
harlanhaskins / projecting.swift
Created April 3, 2021 20:52
Project a UnitPoint to a CGRect
extension CGRect {
func projecting(_ point: UnitPoint) -> CGPoint {
return CGPoint(x: minX + (width * point.x),
y: minY + (height * point.y))
}
}
@harlanhaskins
harlanhaskins / TMNT Symbols
Created July 4, 2020 05:59
All the iOS 14/macOS 11/tvOS 14/watchOS 7 symbols that are singable to the TMNT theme song
AACustomByteStreamOpen
AAEntryACLBlob
AAEntryXATBlob
AAFieldKeySetGetKeyCount
AAHeaderGetKeyIndex
ABMultiValueGetCount
ABPersonViewController
ADCommonDefinitions
ADErrorAdUnloaded
ADErrorLoadingThrottled
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t/Test~partial.swiftmodule -module-name Test -primary-file %s
// RUN: %target-swift-frontend -merge-modules -emit-module -o %t/Test.swiftmodule %t/Test~partial.swiftmodule
// RUN: %target-swift-ide-test -print-module -module-to-print=Test -source-filename=x -I %t | %FileCheck %s
// RUN: %target-swift-frontend -emit-interface-path %t.swiftinterface -enable-resilience -emit-module -o /dev/null %s
// RUN: %FileCheck %s < %t.swiftinterface
// CHECK: func hasClosureDefaultArg(_ x: () -> Void = {
// CHECK-NEXT: })
@harlanhaskins
harlanhaskins / BitArray.swift
Created July 20, 2018 22:04
Swift BitArray that behaves like an array of bools.
import Foundation
extension BinaryInteger {
/// Gets the bit at the specified bit index in the receiver, reading from
/// least to most-significant bit.
///
/// For example,
/// ```
/// 0b0010.bit(at: 0) == false
/// 0b0010.bit(at: 1) == true
@harlanhaskins
harlanhaskins / BitstreamWriter.swift
Created June 19, 2018 16:25
LLVM Bitstream Writer in Swift
import Foundation
typealias UnsignedIntegralType = UnsignedInteger & ExpressibleByIntegerLiteral
struct BitstreamRecord {
private(set) var values = [UInt32]()
mutating func append<IntType: UnsignedIntegralType>(_ int: IntType) {
values.append(UInt32(int))
}
import Foundation
import SwiftSyntax
/*:
# Syntax Rewriting
SwiftSyntax provides a class called `SyntaxRewriter`, which will walk a Syntax tree and
perform transformations over the nodes.
By default, these transformations don't do anything. It's the subclass's job to
@harlanhaskins
harlanhaskins / StringScanner.swift
Last active August 4, 2020 23:45
Swift String Scanner
import Foundation
/// `StringScanner` is a fast scanner for Strings and String-like objects.
/// It's used to extract structured bits from unstructured strings, while
/// avoiding making extra copies of string bits until absolutely necessary.
/// You can build Scanners over Substrings, allowing you to scan
/// parts of strings and use smaller, more specialized scanners to extract bits
/// of that String without needing to reuse another scanner.
public struct StringScanner<Input: StringProtocol> {
let input: Input