SwiftUI:如何将.presentation修饰符用于Alert?获取编译器类型检查错误

时间:2019-06-22 16:47:16

标签: swift xcode swiftui xcode11 swift5.1

我正在将我在UIKit中制作的Quiz应用程序重建为SwiftUI,这很有趣,但是我遇到的最后一个.presentation修饰符遇到了编译器错误。

我不确定该如何分解。看起来很简单。运行2012年的MacBook Pro Retina i7。

@State var showingSaveAlert = false
@State var showingLoadSaveAlert = false


var loadProgress: Alert {
        Alert(title: Text("Load Progress?"), message: Text("Continue from question \(studySaver)?"), primaryButton: .default(Text("Load"), onTrigger: {
            self.loadProgressHandler()
        }), secondaryButton: .cancel())
    }

    var savedAlert: Alert {
        Alert(title: Text("Saved!"), message: Text("Your place has been saved. You may now leave this page without loosing progress."), dismissButton: .cancel())
    }


//MARK: SwiftUI
var body: some View {
        VStack{

        // Bunch of Swift UI Code within the VStack
        ...

            }
            .navigationBarTitle(Text(subject + " Test"), displayMode: .inline)
            .navigationBarItems(trailing:
                Button(action: {
                    self.showingSaveAlert = true
                    self.save()
                    print("Saved")
                }) {
                    Text("Save")
                    }
                    .presentation(showingSaveAlert ? savedAlert : nil)
            )
            .onAppear(){
                print("Test Page Appeared")
                self.showingLoadSaveAlert = true
                self.showingContinue = false
                self.questionLoader()
            }
            .presentation(showingLoadSaveAlert ? loadProgress : nil)


    } //MARK: End of SwiftUI

I would expect it to clear but I get a type-check error on the last modifier .presentation(showingLoadSaveAlert ? loadProgress : nil)

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

The main goal is to get an alert to show up when the View appears asking to load a save for a quiz.

Any thoughts?

1 个答案:

答案 0 :(得分:1)

问题出在这条线上:(和其他警报提示代码)

.presentation(showingSaveAlert ? savedAlert : nil)

警报应绑定到SwiftUI中的Bool

将其更改为此:

.presentation($showingSaveAlert) { savedAlert }