Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Crash in Preview (but not in Simulator) when using fileprivate on a protocol in Swift

Consider this stripped-down example of a view with a view model:

import Observation
import SwiftUI

struct MainView: View {
    @State private var viewModel = ViewModel()
    private let items = [0, 1, 2]

    var body: some View {
        List {
            GroupedItemsView(
                viewModel       : viewModel.groupState,
                groupedItems    : [(0, items)]
            )
        }
    }
}

fileprivate struct GroupedItemsView<ViewModel: GroupsExpandable>: View {
    let viewModel               : ViewModel
    let groupedItems            : [(ViewModel.GroupTag, [Int])]
    
    var body: some View {
        ForEach(groupedItems, id: \.0) { groupTag, items in
            Text("nothing")
        }
    }
}

fileprivate protocol GroupsExpandable: AnyObject {      // HERE
    associatedtype GroupTag: Hashable
}

fileprivate final class GroupState<GroupTag: Hashable>: GroupsExpandable {}

@Observable
fileprivate final class ViewModel {
    var groupState = GroupState<Int>()
}

#Preview {
    MainView()
}

This compiles and runs fine in the Simulator, but it crashes in the Preview. However, when I remove the fileprivate modifier before GroupsExpandable (see HERE), it also runs in the Preview.

What is the reason for this? Bug in Preview? Error on my side somewhere?

Thanks.

System is Xcode Version 16.3 (16E140) on a MacBook Pro 2018 (Intel) running Sequoia 15.3.2 Devices are iPhone 16 Pro Max with iOS 18.3.1, compiler is set to Swift 6.

Answered by Frameworks Engineer in 833929022

Whoa, that's wild! Thanks for providing this reproducer. If you are able, please file a feedback with this information and include the previews diagnostics with the full error for more information. We can use that feedback number and then you can keep track of the resolution.

Accepted Answer

Whoa, that's wild! Thanks for providing this reproducer. If you are able, please file a feedback with this information and include the previews diagnostics with the full error for more information. We can use that feedback number and then you can keep track of the resolution.

Crash in Preview (but not in Simulator) when using fileprivate on a protocol in Swift
 
 
Q