troubleshooting SwiftUI crashes

I'm just starting out with SwiftUI (for macOS) and am running into all sorts of problems, mostly related to resizing the application window.


I'm seeing two distinct crashes in my app, but I can't figure out how to troubleshoot them.


One is incredibly vague:


*** Assertion failure in -[_TtC7SwiftUIP33_A874FC5B9DB530D4375C25AE2AA39DF215HostingClipView setBoundsOrigin:], 
/AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.40.150/AppKit.subproj/NSView.m:5646

this'll happen randomly when resizing the window, but there's nothing else to go on.


The other is:


It's not legal to call -layoutSubtreeIfNeeded on a view which is already being laid out.  If you are implementing 
the view's -layout method, you can call -[super layout] instead. Break on void _NSDetectedLayoutRecursion(void) to 
debug.  This will be logged only once.  This may break in the future.

Which gives me a tip, but once I actually figured out how to set that breakpoint, I still really have no idea what to do. The stack trace for either really doesn't do me any good.

Replies

If it is still a small project, could uou post here:

- ContentView file

- Appdelegate file

@Claude31. I got the same problem (Xcode12)

ContentView.swift:
Code Block swift
import SwiftUI
struct ContentView: View {
    @State var isSideBarPresented = true
    @State var node = Node3D()
    var body: some View {
        VStack {
            Button("Toggle") { isSideBarPresented.toggle() }
            HSplitView {
                Color.white.frame(
                    minWidth: 100,
                    maxWidth: .infinity,
                    minHeight: 0,
                    maxHeight: .infinity
                )
                if isSideBarPresented {
                    Node3DConfigurationView(node: $node)
                }
            }
        }.frame(
            minWidth: 0,
            maxWidth: .infinity,
            minHeight: 0,
            maxHeight: .infinity
        )
    }
}

Node3DConfigurationView.swift:
Code Block swift
import simd
import SwiftUI
struct Node3D {
    var pivot: simd_float3 = .zero
    var position: simd_float3 = .zero
    var rotation: simd_float3 = .zero
    var scale: simd_float3 = .zero
}
/* not sure if needed, but I randomly got some error without it */
fileprivate func wrap(_ binding: Binding<Float>) -> Binding<NSNumber> {
    Binding(
        get: { NSNumber(value: binding.wrappedValue) },
        set: { binding.wrappedValue = $0.floatValue }
    )
}
struct Node3DConfigurationView: View {
    @Binding var node: Node3D
    var body: some View {
        ScrollView(.vertical) {
            Group {
                makeSection("Pivot", value: $node.pivot)
                makeSection("Position", value: $node.position)
                makeSection("Rotation", value: $node.rotation)
                makeSection("Scale", value: $node.scale)
            }
            .textFieldStyle(RoundedBorderTextFieldStyle())
        }
    }
    
    func makeSection(_ headerTitle: String, value: Binding<simd_float3>) -> some View {
        Section(header: makeHeader(headerTitle)) {
            HStack {
                HStack {
                    Text("x")
                    TextField("\(headerTitle)X", value: wrap(value.x), formatter: NumberFormatter())
                }
                .frame(minWidth: 50)
                HStack {
                    Text("y")
                    TextField("\(headerTitle)Y", value: wrap(value.y), formatter: NumberFormatter())
                }
                .frame(minWidth: 50)
                HStack {
                    Text("z")
                    TextField("\(headerTitle)Z", value: wrap(value.z), formatter: NumberFormatter())
                }
                .frame(minWidth: 50)
            }.padding()
        }
    }
    func makeHeader(_ title: String) -> some View {
        Text(title).frame(maxWidth: .infinity, alignment: .leading)
    }
}

AppDelegate.swift (Default, generated by Xcode):
Code Block swift
import Cocoa
import SwiftUI
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow!
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let contentView = ContentView()
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false
        )
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }
}