Latest weblog entries

2021-03-22 Debugging view sizes

Here's a tip if you wish to show the sizes of views in your SwiftUI previews. Add the following struct to your project.

    struct ViewSizePreferenceReader: View {
        private struct ViewSizePreferenceKey: PreferenceKey {
            typealias Value = [CGSize]
            static var defaultValue: [CGSize] = []
            static func reduce(value: inout [CGSize], nextValue: () -> [CGSize]) {
                value.append(contentsOf: nextValue())
        @Binding var sizeString: String
        var body: some View {
            GeometryReader { (geometry: GeometryProxy) in
                    .preference(key: ViewSizePreferenceKey.self, value: [geometry.size])
            .onPreferenceChange(ViewSizePreferenceKey.self) { preferences in
                if let size = preferences.first {
                    self.sizeString = String(format: "%.1f x %.1f", size.width, size.height)

Then add a @State variable for the string, and use the above view as a background, as follows. The preview below will show how the Dynamic Type setting influences SF Symbol based images.

    struct ContentView: View {
        @State private var sizeString = "0x0"
        var body: some View {
            VStack {
                Image(systemName: "magnifyingglass")
                    .background(ViewSizePreferenceReader(sizeString: self.$sizeString))
                Text("Size of button: \(self.sizeString)")
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            Group {
            .previewLayout(.fixed(width: 414, height: 200))

2021-01-22 Error in SwiftUI Previews part deux

Today Xcode refused to show previews for one view, displaying "Cannot preview this file" on top of the previews canvas.

This time it's showing the error "SomeUnitTest.swift must belong to at least one target in the current scheme in order to use previews". I have the feeling that this error is incorrect and masks a problem elsewhere, because unittests most certainly should not belong to any other target than the test target.

I have been able to temporarily fix the problem as follows:

  • Quit Xcode and simulator
  • Delete the ~/Library/Developer/Xcode/DerivedData folder
  • Start Xcode and simulator
  • Remove all unittests from the project (don't move to trash, simply remove references)
  • Compile the project
  • Add the unittests back to the project

2020-07-16 Error in SwiftUI preview

Today, one SwiftUI Preview would not show anything. The Diagnostics button showed the following error:

    GenericHumanReadableError: unexpected error occurred
    noPreviewInfos(arch: "x86_64", sdkRoot: "/Applications/")

After a bit of fiddling, I found out the cause. This project had multiple targets. And the file was not a member of the currently selected target.

Edit 2021-01-15: the above solution was only temporary. Another attempt involved removing all unittests also temporarily "fixed" this problem. At the moment, this seems to be a very rare error and I have not been able to find a definitive cause or solution.

2020-05-27 Fixing a slow build in Xcode

If your Xcode project isn't compiling as fast as you expect, open the build settings of target that you're building. Add the following to the "Other Swift Flags" setting:

    -Xfrontend -warn-long-expression-type-checking=100

This makes the compiler emit warnings when it takes more than 100 ms to type-check an expression.

2020-03-05 Capping an array in Swift

Do you want to cap an array in Swift to a certain size? Here's how:

    var array = ["a", "b", "c", "d", "e"]
    array.replaceSubrange(3..., with: [])

The array now contains three items. It'll crash when instead of 3, you enter an index that's beyond the array count. If you wish to avoid that, do something like the following:

    array.replaceSubrange([0, array.count, 3].sorted()[1]..., with: [])


Weblog Archive

Weblog entries 2021

Weblog entries 2020

Weblog entries 2019

Weblog entries 2018

Weblog entries 2017

Weblog entries 2016

Weblog entries 2015

Weblog entries 2014

Weblog entries 2013

Weblog entries 2012

Weblog entries 2011

Weblog entries 2010

Weblog entries 2009

Weblog entries 2008

Weblog entries 2007

Weblog entries 2006

Weblog entries 2005

Weblog entries 2004

All weblog entries


Articles, chronologically (latest first). This is pretty old stuff.


Not yet finished. Maybe will never be finished. Maybe they'll get deleted. Who knows?


System administration: