Skip to content

Instantly share code, notes, and snippets.

@douglashill
douglashill / Markdown.pl.swift
Created November 26, 2023 21:55
Writes a Markdown document to HTML. The primary goal is for output to match the output of Markdown.pl 1.0.1 as closely as possible.
// `@testable` needed to access `_data`
@testable import Markdown // This package: https://github.com/apple/swift-markdown
/// Writes a Markdown document to HTML.
///
/// The primary goal is for output to match the output of Markdown.pl 1.0.1 as closely as possible.
/// <https://daringfireball.net/projects/markdown/>
///
/// Usage:
///
@douglashill
douglashill / URL+ExtractFragment.swift
Created January 14, 2023 18:24
Splits a URL that might have a fragment into the URL with the fragment removed and the fragment text.
extension URL {
/// Splits a URL that might have a fragment into the URL with the fragment removed and the fragment text.
///
/// If there is no fragment in the URL, this will return a URL identical to the receiver and nil.
///
/// Example input: https://github.com/douglashill/KeyboardKit/blob/main/Features.md#date-picker
/// Example output: (https://github.com/douglashill/KeyboardKit/blob/main/Features.md, date-picker)
func extractFragment() -> (urlWithoutFragment: URL, fragment: String?) {
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
logError("Couldn’t create URL components from URL in order to extract fragment.")
@douglashill
douglashill / message-to-nil-changes-variable-to-nil.m
Created May 24, 2021 15:43
Sending a message to nil is supposed to do nothing in Objective-C, but here it’s changing the local variable error to nil. Tested with Xcode 12.5 on macOS 11.3.1.
NSError *error = [NSError errorWithDomain:@"domain" code:123 userInfo:nil];
[(NSFileHandle *)nil seekToOffset:0 error:&error];
assert(error != nil); // 💥 Not OK. The error has gone!
@douglashill
douglashill / SidebarTest.swift
Created October 9, 2020 18:18
Trivial Catalyst test app to see if `primaryBackgroundStyle = .sidebar` works to get a translucent blurry sidebar on Big Sur
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private var _window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
@douglashill
douglashill / FourColumns.swift
Created June 23, 2020 21:05
A sample UIKit app that sets up a four column layout with new iOS 14 API on UISplitViewController.
import UIKit
class FourColumnsContainerViewController: UIViewController {
let outerSplitViewController = UISplitViewController(style: .tripleColumn)
let innerSplitViewController = UISplitViewController(style: .doubleColumn)
let primary = makeContentViewController("App")
let secondary = makeContentViewController("Files")
let mainContent = makeContentViewController("File Content")
let inspector = makeContentViewController("Inspector")
@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)
@douglashill
douglashill / MenuAlignment.swift
Last active April 15, 2023 15:45
Swizzles the iOS contextual menu and share sheet to improve usability by showing the icon on the leading side. Read more: https://douglashill.co/menu-icon-swizzling/
// Douglas Hill, March 2020
// Code for the article at https://douglashill.co/menu-icon-swizzling/
import UIKit
struct MenuAlignmentFixError: Error, CustomStringConvertible {
let description: String
}
@douglashill
douglashill / extract-most-common-localised-strings.swift
Last active August 24, 2022 05:39
Extracts the most common translations from Apple’s glossary files. Read more: https://douglashill.co/localisation-using-apples-glossaries/
#! /usr/bin/swift
// Douglas Hill, March 2020
/*
Extracts the most common translations from Apple’s glossary files.
This script helped with localisation for KeyboardKit (https://github.com/douglashill/KeyboardKit) by leveraging Apple’s existing translations.
More detail in the article at https://douglashill.co/localisation-using-apples-glossaries/
@douglashill
douglashill / extract-specific-localised-strings.swift
Last active August 24, 2022 05:39
Extracts specific localised strings from Apple’s glossary files. Read more: https://douglashill.co/localisation-using-apples-glossaries/
#! /usr/bin/swift
// Douglas Hill, March 2020
// This file is made available under the MIT license included at the bottom of this file.
/*
Extracts specific localised strings from Apple’s glossary files.
This script helped with localisation for KeyboardKit (https://github.com/douglashill/KeyboardKit) by leveraging Apple’s existing translations.
@douglashill
douglashill / explore-localisation-glossaries.swift
Last active August 24, 2022 05:39
Prints out translations from Apple’s glossary files matching text in a supplied English .strings file. Read more: https://douglashill.co/localisation-using-apples-glossaries/
#! /usr/bin/swift
// Douglas Hill, March 2020
// Prints out translations from Apple’s glossary files matching text in a supplied English .strings file.
// More detail in the article at https://douglashill.co/localisation-using-apples-glossaries/
import Foundation
let stringsSource = URL(fileURLWithPath: "PUT THE PATH TO YOUR ENGLISH .strings FILE HERE")