获取错误:表达式列表中的预期表达
我可以使用开关检查变量并根据变量的值实现视图。我尝试使用其他情况,但仍然遇到相同的错误。我是否必须创建一种方法并返回相同的视图并在此处使用它?
struct AppThemeButton: View {
var action: (() -> Swift.Void)?
var buttonType: ThemeButtonType = .bordered
var body: some View {
Button {
// button action
if let act = action {
act()
}
} label: {
Text("+ \(TextStrings.addAProject.localized())")
.frame(maxWidth: .infinity, maxHeight: .infinity,
alignment: .center)
.background(
switch self.buttonType {
case .bordered:
Color.green
case .colored:
Color.red
}
)
.frame(height: 60, alignment: .center)
.padding([.leading, .trailing])
}
}
}
enum ThemeButtonType {
case bordered
case colored
}
Can i not use switch to check for a variable and implement a view based on the variable's value. I tried using if else as well but still getting the same error. Do I have to create a method and return a view for the same and use it here?
struct AppThemeButton: View {
var action: (() -> Swift.Void)?
var buttonType: ThemeButtonType = .bordered
var body: some View {
Button {
// button action
if let act = action {
act()
}
} label: {
Text("+ \(TextStrings.addAProject.localized())")
.frame(maxWidth: .infinity, maxHeight: .infinity,
alignment: .center)
.background(
switch self.buttonType {
case .bordered:
Color.green
case .colored:
Color.red
}
)
.frame(height: 60, alignment: .center)
.padding([.leading, .trailing])
}
}
}
enum ThemeButtonType {
case bordered
case colored
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用此修饰符 https:// https://developer.apple。 com/documation/swiftui/view/background(_:对齐:) ,它需要
view
作为参数而不是函数或闭合。自iOS 15以来,修饰符被弃用。如果您的应用程序针对iOS 15及以上,则可以使用此新修饰符 https://developer.apple.com/documentation/swiftui/view/background/background(Alignment:content:content :)
如果以下iOS 15,则应通过包装您的标签您的
切换器
结果
You're using this modifier https://developer.apple.com/documentation/swiftui/view/background(_:alignment:), it requires a
View
as parameter not a function or closure.The modifier is deprecated since iOS 15. If your app is targeted iOS 15 and above, you can use this new modifier https://developer.apple.com/documentation/swiftui/view/background(alignment:content:)
In case of below iOS 15, you should wrap your label by your
switcher
Result
或者更短
Or Even Shorter