Archive for March 13, 2023

Monday, March 13, 2023

Pattern Matching on Swift Error Codes

Ole Begemann:

I was wondering why this shorter syntax works. Is there some special compiler magic for pattern matching against error codes of NSError instances? Turns out: no, the answer is much simpler. Foundation includes an overload for the pattern matching operator ~= that matches error values against error codes.

The implementation looks something like this:

public func ~= (code: CocoaError.Code, error: any Error) -> Bool {
    guard let error = error as? CocoaError else { return false }
    return error.code == code
}

However, my experience has been that this only works for concrete types, not when your errors or codes use a protocol.

Previously:

Update (2023-03-15): See also: Rob Jonson.

Update (2023-03-16): The problem seems to be this bug (via Rob Jonson).

Ventura Time Machine Backups and the APFS Uncertainty Principle

Howard Oakley:

Before making any snapshots for this backup, TMA performs housekeeping on its own previous snapshots, both on each volume being backed up, and on the target volume (backup storage). The rule it applies is inflexible: it deletes any local snapshots (on a volume being backed up) that are more than 24 hours old, unless it’s the last snapshot on that volume, which it retains for this backup. Even so, TMA snapshots can become orphaned and left indefinitely, for the user to spot and delete manually. Although uncommon, this can occur when Macs are unable to make full backups because they’re disconnected from their backup storage for more than 24 hours at a time.

[…]

TMA next has the task of deciding how it’s going to work out what needs to be backed up on each source volume. […] Snapshot deltas were first choice in the past, but TMA has returned to preferring FSEvents when possible, for second and subsequent backups of a volume.

I wonder whether this is for performance reasons.

Howard Oakley:

If the large files you put in the Trash were on the volume being backed up when Time Machine made one of those hourly snapshots, then the data for those files will be retained as part of that snapshot, until the last snapshot containing those files is deleted. Only then can the data for those files be removed, and the space they took up will be freed.

[…]

The cleanest way to avoid this happening is to keep all downloads and large temporary files on a separate volume which isn’t backed up, so doesn’t have snapshots made of it automatically.

[…]

If you don’t want to use a BigFiles volume, or can’t select where to save a large temporary file, you can work around snapshots by timing. Before acquiring that large file, check when the last Time Machine backup was made, using the Time Machine item in the menu bar. Provided there’s sufficient time before the next backup is due, you can download the large file(s), move them to another disk for storage or otherwise use them, then put them in the Trash. But the most essential step is that you must then empty the Trash before the next backup starts, or they’ll be captured in the next automatic snapshot.

Adam Engst:

I’ve been pondering just how difficult—perhaps impossible—it is to know how much free space you actually have on a Mac’s drive these days (see “Ensure Sufficient Free Space before Upgrading to Ventura,” 15 November 2022 and “iPhones and iPads Now Require a Passcode on Every Backup/Sync,” 11 January 2023). The complexity underlying APFS and macOS in general creates a situation where the amount of free space isn’t entirely deterministic.

[…]

I figured that if I emptied the Trash, I’d either see no change due to the Time Machine snapshot issue or have almost exactly 145 GB free. Surprisingly, neither was true. After a minute or so, the free space number did rise—as expected—but first to 148 GB (not shown) before settling down at 146 GB. I can’t explain why it was a full gigabyte higher than would seem possible.

[…]

Grasping at straws, I decided to restart, just in case that somehow helped the filesystem come to a better conclusion about the amount of free space. Unfortunately, after my Mac came back up (with the same apps running), the free space number had dropped to 159 GB.

[…]

I no longer believe it’s possible to audit the free space on a Mac or to explain precisely how much space should be freed up by a particular action.

Previously:

Indispensable Developer Tools

David Smith:

This isn’t comprehensive, but my hope is to surface potentially less well known tools that I have found super helpful. The obvious ones like Xcode and Terminal are obviously essential, if not particularly interesting.

Sim Genie is an absolutely required tool for anyone doing development involving GPS. Curtis has put together a fantastic tool that can do all manner of things for improving the iOS simulator, but its GPS Simulator function is amazing. You load up a GPX file and it will play it back and various speeds and various rates.

[…]

Pastel is essential for anything involving color (which is probably every app).

[…]

The iOS Simulator’s video recording can export gifs, but I can’t easily edit those outputs. So instead I would often record to video there, then roundtrip quickly to Final Cut to tweak something, then export them in Gifski for sharing.

[…]

I was constantly juggling GPX files for this update and found the best place to preview/compare them was the website GPX Studio.

From this article, I use Soulver, Sketch, and Kaleidoscope a lot. Other key apps for development: BBEdit, Dash, Tower, Hopper, LaunchBar (opening headers, snippets for coding and customer support), DropDMG, Script Debugger, EagleFiler (issue tracking, customer support history, and documentation searching), Apparency, and Base.

Previously: