A brief introduction to type memory layout in Swift

Most of the time we Swift programmers don’t really think about how our types – structs, classes, actors, enums, etc – are represented in memory. We just deal with them abstractly, knowing that somehow the compiler boils them down to a bunch of bytes, handling all the complexity of wrangling those bytes for us. However,… Read more

Swift `print` takes multiple arguments

I forget this too often, and write things like: Sometimes I feel like I’m really just writing stress-tests for the Swift syntax parser. The above can actually be written a bit more simply, as: Not just fewer characters, but conceptually simpler – fewer nested parenthesis, and no nested string literals at all. See, print‘s function… Read more

Hiding SwiftUI views

There are several ways to hide a SwiftUI view, although they don’t all agree on what it means to hide the view. Do you want it to be invisible, or actually not there? To make it invisible you need only set its opacity to zero or use the hidden modifier. But the view will still… Read more

Module verification must be enabled in order for Swift to use the module

Ugh. This was annoying to figure out. If you have a framework target in Xcode with a modulemap – e.g. because you’re wrapping a C or C++ library for use in Swift – you must keep the module verifier enabled (the ENABLE_MODULE_VERIFIER build setting) for that framework, otherwise any Swift targets using that framework won’t… Read more

SwiftUI drag & drop does not support file promises

SwiftUI doesn’t offer anything equivalent to NSFilePromiseProvider, i.e. to write data to the drop destination. You have to ditch SwiftUI and use AppKit’s drag & drop APIs instead. FB13583826. Is that it? I know that’s not a very helpful in some sense, but I wasted days trying to figure out how to implement this very… Read more

Bad API example: FileManager’s url(for:in:appropriateFor:create:)

I find FileManager‘s url(for:in:appropriateFor:create:) to be very unintuitive. It seems to have multiple, largely-orthogonal functions. It can provide paths to common folders (albeit badly). It can create temporary folders. It can locate volume-specific bins (Trash folders). It is an example of bad API design. Specifically, regarding cohesion: the principle that an API should have one… Read more

Creating files safely in Mac apps

Creating a file is a pretty basic and conceptually simple task, that many applications do (whether they realise it or not – library code often does this too, at least for temporary files such as caches or for communicating between programs). So you’d think it’d be trivial to do correctly. Alas, it is not. ☝️… Read more