Swift tip: the swap function

The following code prints the Fibonacci sequence. You’ve probably seen it before. It’s one of the simplest and most well-known examples of a sliding window operation – where the next value depends on the preceding two (or more) values. While almost all programs do not calculate the Fibonacci sequence, many do contain similar sliding-window algorithms.… Read more

URLSession performance for reading a byte stream

What’s the best way to read a stream of bytes with URLSession? That’s the simple question I set out to answer. I wrote some benchmarks. They read a 128 MiB file and perform a contrived aggregation of its content bytes (a joking “hash” of them, merely to ensure the actual reads aren’t optimised out). ⚠️… Read more

Swift’s native Clocks are very inefficient

Screenshot of Instruments showing the outline view for a Time Profile, expanded to show dozens of spurious, overhead functions taking up the vast majority of the runtime.

By which I mean, things like ContinuousClock and SuspendingClock. In absolute terms they don’t have much overhead – think sub-microsecond for most uses. Which makes them perfectly acceptable when they’re used sporadically (e.g. only a few times per second). However, if you need to deal with time and timing more frequently, their inefficiency can become… Read more

Matching prefixes in Swift strings

How do you determine if one string starts with another, in Swift? Surely that’s exactly what the hasPrefix(_:) method is for: No can haz etiquette? Wot? The problem is that hasPrefix is not meant for general use with human text; it’s barely better than a byte-wise comparison. It only guarantees that it won’t be fooled… Read more

getBitmapDataPlanes can break NSImages & NSBitmapImageReps

Opaque black square.

Today was one of those days where you plan to real make a dent in your todo list, and end up spending the entire day debugging why the hell some images are suddenly rendering as completely opaque black. Long story short, on at least some bitmap images, as soon as you call getBitmapDataPlanes it somehow… Read more

Including Services in contextual menus in SwiftUI

Screenshot of the good Services submenu (as found in the application menu), with more options, app icons, and better grouping.

SwiftUI provides a way to provide a contextual menu for a view, with contextMenu(menuItems:) and friends, but it requires you to manually specify the entire contents of the contextual menu. That means it does not include the standard Services submenu. A brief history of Contextual Menus Contextual menus were introduced [to the Mac] in 1997… Read more

no platform load command found in ‘libxyz.a’, assuming: macOS

This is a linker warning I see frequently since Xcode 15.0. It appears it’s a result of Apple’s new linker, “ld_prime”, which replaced “ld64” that was in use [by Apple] since around 2005 (per Quinn the Eskimo). ☝️ “ld_prime” might be an internal code name, or perhaps is just Quinn’s personal nomenclature. The actual binary… Read more