Skip to content

Instantly share code, notes, and snippets.

View tonyarnold's full-sized avatar

Tony Arnold tonyarnold

View GitHub Profile
import Foundation
extension MainActor {
/// Invoke `body`, running synchronously if possible.
///
/// This method is equivalent to `Task { @MainActor in <body> }`, except that
/// the first thread hop is elided if the caller is already on the main thread.
/// Thus if `<foo>` has no subsequent thread hops, it can run fully synchronously.
@discardableResult
public static func runAsap<Success>(
@sharplet
sharplet / run.sh
Created April 19, 2021 14:26
Use bin/run to run Swift commands found in a project's bin/ directory
#!/bin/sh
#
# bin/run: Run Swift commands in a project's bin/ directory.
#
# Drop-in replacement for `swift run`.
# Adds caching of built products based on source file contents.
#
# Usage:
# bin/run <command [options]> # Run named command defined in `bin/Package.swift`
#
@helje5
helje5 / SparkleCommands.swift
Last active January 11, 2023 00:37
How to hookup Sparkle in SwiftUI
//
// SparkleCommands.swift
// Past for iChat
//
// Created by Helge Heß on 08.04.21.
//
import SwiftUI
#if SPARKLE && canImport(Sparkle)
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@harlanhaskins
harlanhaskins / swift-format.swift
Created October 26, 2017 19:48
Simple Swift Formatter using SwiftSyntax
import Foundation
import SwiftSyntax
func main() throws {
guard CommandLine.arguments.count > 1 else {
print("usage: swift-format [file]")
exit(-1)
}
let url = URL(fileURLWithPath: CommandLine.arguments[1])
@khanlou
khanlou / Collection+Chunking.swift
Last active December 23, 2020 02:00
Collection chunking via @timvermeulen
extension Collection {
func chunk(size: Int) -> ChunkedCollection<Self> {
ChunkedCollection(self, size: size)
}
}
struct ChunkedCollection<Base: Collection>: Collection {
private let base: Base
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)
@chriseidhof
chriseidhof / sample.swift
Last active December 6, 2019 22:52
Observable References
import Foundation
// A lens is a getter and a setter combined
struct Lens<Whole, Part> {
let get: (Whole) -> Part
let set: (inout Whole, Part) -> ()
}
// We can create a lens from a key path
extension Lens {
@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
@alloy
alloy / gist:6453958
Last active December 22, 2015 09:48
Prototype of an assertion that checks a NSManagedObject is being used on the queue belonging to its NSManagedObjectContext. This should obviously only be used during development.
/////////// Header
#import <CoreData/CoreData.h>
@interface NSManagedObject (FTCoreDataQueueContainmentAssertions)
+ (void)FTCDQCA_enableAssertions:(Class)klass;
@end
/////////// Implementation