Crash When Previewing w/ Struct Binding Reassignment In Constructor

Platform Specs:

  • Xcode 16.2
  • Swift 6.0.3
  • iOS 18.2 + iOS Simulator 18.3.1

Issue:

Refer to the following code:

struct CustomView: View {
	@Binding var prop: CustomStruct
	
	init(prop p: Binding<CustomStruct>) {
		_prop = p
	}
	
	init(isPreview: Bool) {
		let p = CustomStruct()
		_prop = .constant(p)
	}
	
	var body: some View {
		VStack {
			Text("hi")
		}
	}
}
	
#Preview {
	CustomView(isPreview: true)
		.preferredColorScheme(.dark)
}

The first constructor is for normal app functionality (and previews/functions correctly when used with the rest of the app in the ContentView preview tab). The second constructor is for previewing only CustomView in its own preview tab. This constructor does not work when previewing in the same file, as shown above. It triggers an ambiguous crash, stating that the diagnostic log (which obviously provides no clear information) should be checked.

I have isolated the issue to be in the Binding reassignment in the second constructor. Replacing CustomStruct with anything but another struct, like an enum or primitive, fixes the issue.

Note: This bug only occurs when previewing (either through the #Preview macro or PreviewProvider struct).

Answered by RyloRiz in 828017022

This is not a bug. After extensive line-by-line and case-by-case testing, I realized I misunderstood how initializing default values in SwiftData works.

You can read more about the issue here:

  • https://fatbobman.com/en/posts/relationships-in-swiftdata-changes-and-considerations/#the-misconception-of-setting-default-values-for-relationships

Hi,

Sorry to hear you are having problems getting previews working. I attempted to reproduce, but this code loads a preview fine for me:

import SwiftUI

struct CustomStruct {}

struct CustomView: View {
    @Binding var prop: CustomStruct
    
    init(prop p: Binding<CustomStruct>) {
        _prop = p
    }
    
    init(isPreview: Bool) {
        let p = CustomStruct()
        _prop = .constant(p)
    }
    
    var body: some View {
        VStack {
            Text("hi")
        }
    }
}

#Preview {
    CustomView(isPreview: true)
        .preferredColorScheme(.dark)
}

We will need the diagnostics Xcode Previews generates in order to make sure we understand the error the previews system is encountering.

Install the logging profile using instructions available here: https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift On your mac running Xcode, and on your physical preview device (if you are using one).

Install the logging profile using the following instructions on your mac running Xcode; and if you are using one, your physical preview device (iOS or visionOS): https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift

Then when you reproduce the problem in Xcode:

  1. Either (a) an error banner will appear, click the "Diagnostics" button in that banner; or (b) if you're not seeing an error but you still want to provide diagnostics you can get the same diagnostics window by going under the Editor menu in the menu bar, then selecting the Canvas submenu, then selecting "Diagnostics".
  2. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  3. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)
  4. Generate a sysdiagnose on your mac and any on-device preview devices, and attach those too
  5. Attach your sample project too, if possible
Accepted Answer

This is not a bug. After extensive line-by-line and case-by-case testing, I realized I misunderstood how initializing default values in SwiftData works.

You can read more about the issue here:

  • https://fatbobman.com/en/posts/relationships-in-swiftdata-changes-and-considerations/#the-misconception-of-setting-default-values-for-relationships
Crash When Previewing w/ Struct Binding Reassignment In Constructor
 
 
Q