gpt4 book ai didi

ios - (Xcode 12/iOS 14) 如何解决调用 App.Alert 和 SwiftUI.Alert 的兼容性错误?

转载 作者:行者123 更新时间:2023-12-04 08:40:17 25 4
gpt4 key购买 nike

ContentView我在 var body: some View 中定义的 View 中有以下代码:

.alert(item: $alertItem) { (alert) -> Alert in
Alert(
title: Text(alert.title),
message: Text(alert.message)
)
}
前两行分别抛出以下两个编译器错误:
Declared closure result 'App.Alert' is incompatible with contextual type 'SwiftUI.Alert'
Extra arguments at positions #1, #2 in call
我有一个定义 AlertItem 的结构,如下所示:
struct AlertItem: Identifiable {    // *
var id = UUID()
var title: String
var message: String
}
如何解决兼容性错误,使其符合 SwiftUI.Alert?任何建议将不胜感激。

最佳答案

您可以尝试以下操作:

.alert(item: $alertItem) { alert in
SwiftUI.Alert(
title: Text(alert.title),
message: Text(alert.message)
)
}

关于ios - (Xcode 12/iOS 14) 如何解决调用 App.Alert 和 SwiftUI.Alert 的兼容性错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64599644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com