Canvas Fails to build ContentView.swift in SwiftUI Project

Viewed 283

UPDATE: I am using a library called ASCollectionView and I have discovered that replacing the first content view struct in this example with a plain Text view will allow canvas to build as expected. If I replace the second content view struct in this example with a Text view, canvas still fails to build. I have completely replaced the failing struct with the struct that works; even going so far as to limiting it to two ASCollectionViewSections as in the working struct, but for some reason that specific struct still causes canvas preview to fail to build. I thought maybe it was a collision on the section IDs since I had both start at 0, but changing the second collection to use 3 and 4 instead of restarting to 0 and 1 does not fix the issue. I see no difference between these two struct other than one has three sections and the other two. But as I stated, I have tried each on their own and even limited the failing collection to just two sections with no change in behavior.

I am running Xcode Version 12.5.1 (12E507)

I found tons of results for the cause of this and various fixes on google and previously asked stack overflow questions. But none of the fixes I found have worked; many were for older versions of Xcode or not specific to SwiftUI, and I am relatively new to iOS development. I decided to make a simple iOS app in SwiftUI to try out the framework and learn a little more about iOS development. So please forgive me if this is a simple error on my part, but my project builds and runs on simulators and real devices with no issue. However, when I attempt to refresh the canvas it fails to build ContentView.swift and tells me to try breaking up the expression.

The body of my ContentView holds a TabView and I have moved the content for each tab; as well as the @State vars used to modify and set the values of the elements in the views, into their own structs within my ContentView struct. Everything is still building and installing as well as it did when I had it all in the main body of the ContentView struct, but canvas still fails. This is the output of the diagnostic window on Canvas.

I have even tried commenting our the entire content of my root ContentView body and only returning a 'Text()' view, but the issue persists.

Any time and/or help anyone can provide on this issue is greatly appreciated and hopefully I will learn a thing or two about iOS development; or at least Xcode, by the time I get this solved.

Code Example

struct ContentView: View {
    @State private var selectedTab = 0
    
    init() {
        UITabBar.appearance().backgroundColor = UIColor(Color("color-background"))
        UITabBarAppearance().backgroundColor = UIColor(Color("color-background"))
    }
    
    var body: some View {
        TabView() {
            OtherItemViewContent().onTapGesture {
                print("tapped Primary")
                self.selectedTab = 1
            }.tabItem {
                Label{
                    Text("Primary")
                } icon: {
                    Image("primary")
                        .renderingMode(.template)
                }
            }.tag(1)
            
            ListItemContentView().onTapGesture {
                self.selectedTab = 2
            }.tabItem {
                Label{
                    Text("Secondary")
                } icon: {
                    Image("secondary")
                        .renderingMode(.template)
                }
            }.tag(2)
        .foregroundColor(Color("color-text"))

    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView().environmentObject(Data_API())
        }
    }
}

struct OtherItemViewContent: View {
@State private var otherItemOneList = ListItem
@State private var otherItemTwoList = ListItem
@State private var otherItemThreeList = ListItem

var body: some View {
    ASCollectionView {
        ASCollectionViewSection(id: 0, data: otherItemOneList, dataID: \.self) { item, _ in
            HStack(alignment: .center) {
                VStack(alignment: .center) {
                    Text("Rank")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("#\(item.id)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 30)
                VStack(alignment: .center) {
                    Text("Name")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.itemName)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 130)
                VStack(alignment: .center){
                    Text("Pick Ratio")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.pickRatio)%")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 150)
                VStack(alignment: .center) {
                    Text("KD")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.kdRatio)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 40)
            }
            Divider()
        }
        .sectionHeader {
            Text("Items One")
                .foregroundColor(Color("color-text"))
                .padding()
        }
        ASCollectionViewSection(id: 1, data: otherItemTwoList, dataID: \.self) { item, _ in
            HStack(alignment: .center) {
                VStack(alignment: .center) {
                    Text("Rank")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("#\(item.id)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 30)
                VStack(alignment: .center) {
                    Text("Name")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.itemName)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 130)
                VStack(alignment: .center){
                    Text("Pick Ratio")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.pickRatio)%")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 150)
                VStack(alignment: .center) {
                    Text("KD")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.kdRatio)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 40)
            }
            Divider()
        }
        .sectionHeader {
            Text("Items Two")
                .foregroundColor(Color("color-text"))
                .padding()
        }
        ASCollectionViewSection(id: 2, data: otherItemThreeList, dataID: \.self) { item, _ in
            HStack(alignment: .center) {
                VStack(alignment: .center) {
                    Text("Rank")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("#\(item.id)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 30)
                VStack(alignment: .center) {
                    Text("Name")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.itemName)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 130)
                VStack(alignment: .center){
                    Text("Pick Ratio")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.pickRatio)%")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 150)
                VStack(alignment: .center) {
                    Text("KD")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.kdRatio)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 40)
            }
            Divider()
        }
        .sectionHeader {
            Text("Items Three")
                .foregroundColor(Color("color-text"))
                .padding()
        }
    }
    .layout { sectionID in
        switch sectionID {
            default:
                return .list(
                    itemSize: .absolute(50),
                    spacing: 10,
                    sectionInsets: .zero,
                    insetSupplementaries: true,
                    stickyHeader: false,
                    stickyFooter: false)
        }
    }
    .onAppear() {
        MY_API().loadOtherItemsData { (otherItemOneList, otherItemTwoList, otherItemThreeList) in
            self.otherItemOneList = otherItemOneList
            self.otherItemTwoList = otherItemTwoList
            self.otherItemThreeList = otherItemThreeList
        }
    }
}
}

struct ListItemsViewContent: View {
@State private var dataOneList = ListItem
@State private var dataTwoList = ListItem

var body: some View {
    ASCollectionView {
        ASCollectionViewSection(id: 3, data: dataOneList, dataID: \.self) { item, _ in
            HStack(alignment: .center) {
                VStack(alignment: .center) {
                    Text("Rank")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("#\(item.id)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 30)
                VStack(alignment: .center) {
                    Text("Name")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.itemName)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 130)
                VStack(alignment: .center){
                    Text("Pick Ratio")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.pickRatio)%")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 150)
                VStack(alignment: .center) {
                    Text("KD")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.kdRatio)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 40)
            }
            Divider()
        }
        .sectionHeader {
            Text("Section One")
                .foregroundColor(Color("color-text"))
                .padding()
        }
        ASCollectionViewSection(id: 4, data: dataTwoList, dataID: \.self) { item, _ in
            HStack(alignment: .center) {
                VStack(alignment: .center) {
                    Text("Rank")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("#\(item.id)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 30)
                VStack(alignment: .center) {
                    Text("Name")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.itemName)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 130)
                VStack(alignment: .center){
                    Text("Pick Ratio")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.pickRatio)%")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 150)
                VStack(alignment: .center) {
                    Text("KD")
                        .fontWeight(.bold)
                        .font(.system(size: 10))
                        .multilineTextAlignment(.center)
                    Text("\(item.kdRatio)")
                        .font(.system(size: 12))
                        .multilineTextAlignment(.center)
                }
                .frame(width: 40)
            }
            Divider()
        }
        .sectionHeader {
            Text("Section Two")
                .foregroundColor(Color("color-text"))
                .padding()
        }
    }
    .layout { sectionID in
        switch sectionID {
            default:
                return .list(
                    itemSize: .absolute(50),
                    spacing: 10,
                    sectionInsets: .zero,
                    insetSupplementaries: true,
                    stickyHeader: false,
                    stickyFooter: false)
        }
    }
    .onAppear() {
        MY_API().loadSecondItemsData { (dataOneList, dataTwoList) in
            self. dataOneList = dataOneList
            self. dataTwoList = dataTwoList
        }
    }
}
}

Diagnostics

the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

----------------------------------------

CompileDylibError: Failed to build ContentView.swift

Compiling failed: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

/Users/<user-name>/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator/SwiftUiPracticeApp.build/Objects-normal/arm64/ContentView.2.preview-thunk.swift:150:76: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
    @_dynamicReplacement(for: body) private var __preview__body: some View {
                                                                           ^


==================================

|  BuildInvocationError
|  
|  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -F 
/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator -enforce-exclusivity=checked -DDEBUG
 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs
/iPhoneSimulator14.5.sdk -target arm64-apple-ios14.0-simulator -module-cache-path /Users/<user-name>
/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -Xfrontend -serialize-debugging-options 
-enable-testing -swift-version 5 -I /Users/<user-name>/Library/Developer/Xcode/DerivedData
/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex/Previews
/SwiftUiPracticeApp/Products/Debug-iphonesimulator -F /Users/<user-name>/Library/Developer/Xcode
/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex/Previews
/SwiftUiPracticeApp/Products/Debug-iphonesimulator -c -j8 -serialize-diagnostics 
-Xcc -I/Users/<user-name>/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze
/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator
/SwiftUiPracticeApp.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/<user-name>/Library
/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build
/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator/SwiftUiPracticeApp.build
/SwiftUiPracticeApp-generated-files.hmap -Xcc -I/Users/<user-name>/Library/Developer/Xcode
/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex
/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator
/SwiftUiPracticeApp.build/SwiftUiPracticeApp-own-target-headers.hmap -Xcc -I/Users/<user-name>
/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build
/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build
/Debug-iphonesimulator/SwiftUiPracticeApp.build/SwiftUiPracticeApp-all-non-framework-target-headers.hmap
 -Xcc -ivfsoverlay -Xcc /Users/<user-name>/Library/Developer/Xcode
/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex
/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator
/SwiftUiPracticeApp.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/<user-name>
/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze
/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build
/Debug-iphonesimulator/SwiftUiPracticeApp.build/SwiftUiPracticeApp-project-headers.hmap -Xcc 
-I/Users/<user-name>/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze
/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Products/Debug-iphonesimulator/include 
-Xcc -I. -Xcc -ISwiftUiPracticeApp -Xcc -ISwiftUiPracticeAppTests -Xcc -ISwiftUiPracticeAppUITests 
-Xcc "-ISwiftUiPracticeApp/Preview Content" -Xcc -I/Users/<user-name>/Library
/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze
/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build
/Debug-iphonesimulator/SwiftUiPracticeApp.build/DerivedSources-normal/arm64 -Xcc -I
/Users/<user-name>/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze
/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build
/Debug-iphonesimulator/SwiftUiPracticeApp.build/DerivedSources/arm64 -Xcc -I/Users/<user-name>
/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex
/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator
/SwiftUiPracticeApp.build/DerivedSources -Xcc -DDEBUG=1 -working-directory /Users
/<user-name>/XcodeProjects/SwiftUiPracticeApp /Users/<user-name>/Library/Developer/Xcode/DerivedData
/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze/Build/Intermediates.noindex/Previews
/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build/Debug-iphonesimulator
/SwiftUiPracticeApp.build/Objects-normal/arm64/ContentView.2.preview-thunk.swift -o /Users
/<user-name>/Library/Developer/Xcode/DerivedData/SwiftUiPracticeApp-gagrbujnkjvcthadtkeswjidloze
/Build/Intermediates.noindex/Previews/SwiftUiPracticeApp/Intermediates.noindex/SwiftUiPracticeApp.build
/Debug-iphonesimulator/SwiftUiPracticeApp.build/Objects-normal/arm64
/ContentView.2.preview-thunk.o -module-name SwiftUiPracticeApp_PreviewReplacement_ContentView_2
 -Onone -Xfrontend -disable-modules-validate-system-headers
0 Answers
Related