【发布时间】:2021-06-29 11:55:28
【问题描述】:
我正在尝试抽象出一个在我的应用程序的多个地方使用的警报。
我复制并粘贴了func alert(isPresented: Binding<Bool>, content: () -> Alert) -> some View 的实现并对其进行了调整以适应我的使用:
extension View {
func externalURLAlert(isPresented: Binding<Bool>, action: ()) -> some View {
isPresented.wrappedValue ? AnyView(Alert(
title: Text("alert.externalURL.title".localized),
message: Text("alert.externalURL.message".localized),
primaryButton: .cancel(),
secondaryButton: .default(Text("alert.externalURL.openAction.title".localized)) {
action
}
)) : AnyView(EmptyView())
}
}
我的计划是在像 .externalURLAlert(isPresented: $isPresented, action: someAction) 这样的视图上调用它,但我无法编译该函数。
我得到的错误如下:
Initializer 'init(_:)' 要求 'Alert' 符合 'View'
【问题讨论】: