Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
airspeedswift / list.swift
Created March 23, 2024 18:48
A Basic Noncopyable Singly Linked List in Swift
// To run this code on a Mac with Xcode installed:
// Download the latest toolchain from https://www.swift.org/download/#trunk-development-main and install it
// From a command line:
// export TOOLCHAINS=`plutil -extract CFBundleIdentifier raw -o - /Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist`
// xcrun swiftc -parse-as-library -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature MoveOnlyPartialConsumption -Xfrontend -disable-round-trip-debug-types -enable-experimental-feature BorrowingSwitch linkedlist.swift
struct Box<Wrapped: ~Copyable>: ~Copyable {
private let pointer: UnsafeMutablePointer<Wrapped>
@schacon
schacon / better-git-branch.sh
Created January 13, 2024 18:41
Better Git Branch output
#!/bin/bash
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NO_COLOR='\033[0m'
@schacon
schacon / git-related-files.rb
Last active May 30, 2024 13:31
git-related-files.sh
//
// A Swift property wrapper for adding "indirect" to struct properties.
// Enum supports this out of the box, but for some reason struct doesn't.
//
// This is useful when you want to do something recursive with structs like:
//
// struct Node {
// var next: Node?
// }
//
@importRyan
importRyan / whenHovered.md
Last active May 11, 2024 03:13
Reliable SwiftUI mouse hover

Reliable mouseEnter/Exit for SwiftUI

Kapture 2021-03-01 at 14 43 39

On Mac, SwiftUI's .onHover closure is not always called on mouse exit, particularly with high cursor velocity. A grid of targets or with finer target shapes will often have multiple targets falsely active after the mouse has moved on.

It is easy to run back to AppKit's safety. Below is a SwiftUI-like modifier for reliable mouse-tracking. You can easily adapt it for other mouse tracking needs.

import SwiftUI
@IanKeen
IanKeen / Example_Complex.swift
Last active January 23, 2024 07:53
PropertyWrapper: @transaction binding for SwiftUI to make changes to data supporting commit/rollback
struct User: Equatable {
var firstName: String
var lastName: String
}
@main
struct MyApp: App {
@State var value = User(firstName: "", lastName: "")
@State var showEdit = false
@nicklockwood
nicklockwood / main.swift
Created May 18, 2020 22:58
A simple one-file language parser and compiler test
import Foundation
enum LexingError: Error, Equatable {
case syntaxError(String)
case unexpectedEOF
}
enum ParsingError: Error {
case expected(String)
case unexpectedToken(Token)
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))

Debugging the Swift Toolchain

Use these steps to debug components of the Swift toolchain. This allows you to see Swift's source code from the debugger – instead of disassembly. The debugger can also provide some variable names and values. This has been initially tested with libswiftCore.dylib.

These instructions were updated as of Swift 5.2.1.

Prerequisites

@BadPirate
BadPirate / README.md
Created January 28, 2020 19:22
A utility class for capturing iOS App Will Suspend, and App Did Un-suspend events

Summary

iOS doesn't report when an app will be suspended (placed from background into a non-processing state) nor does it seem to fire a notification once the app has resumed. There is some confusion about this as there is a notification when the app becomes "active" or will resign the "active" state, however this is not always the right value needed. iOS Apps have a number of states:

  1. Active: App is in the foreground (frontmost) and there are no notifications or menu's pulled over it. Pulling a menu down or getting an external notification or text message will cause the app to "resign" active, and resume active once the alert has been dealt with.
  2. Background: App is not in the foreground but still processing. This happens briefly before suspend if there are no background tasks running, or can be a permanent state if there is a long running background mode (audio, location, etc) running.