获取错误:表达式列表中的预期表达

发布于 2025-01-18 13:27:30 字数 1145 浏览 0 评论 0原文

我可以使用开关检查变量并根据变量的值实现视图。我尝试使用其他情况,但仍然遇到相同的错误。我是否必须创建一种方法并返回相同的视图并在此处使用它?

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
}

Code with error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2025-01-25 13:27:30

您正在使用此修饰符 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,则应通过包装您的标签您的切换器

enum ThemeButtonStyle: ButtonStyle {
    case filled
    case bordered
    
    func makeBody(configuration: Configuration) -> some View {
        switch self {
        case .filled:
            configuration.label
                .foregroundColor(.white)
                .padding()
                .background(Capsule().fill(.red))
        case .bordered:
            configuration.label
                .foregroundColor(.black)
                .padding()
                .background(Capsule().stroke(.black))
        }
    }
}

struct AppThemeButton: View {
    let style: ThemeButtonStyle
    var body: some View {
        Button {
            print("Touched")
        } label: {
            Text("Touch Me")
        }.buttonStyle(style)
    }
}

struct ThemeButtonStyle_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            AppThemeButton(style: .filled)
            AppThemeButton(style: .bordered)
        }
    }
}

结果

”在此处输入图像说明”

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

enum ThemeButtonStyle: ButtonStyle {
    case filled
    case bordered
    
    func makeBody(configuration: Configuration) -> some View {
        switch self {
        case .filled:
            configuration.label
                .foregroundColor(.white)
                .padding()
                .background(Capsule().fill(.red))
        case .bordered:
            configuration.label
                .foregroundColor(.black)
                .padding()
                .background(Capsule().stroke(.black))
        }
    }
}

struct AppThemeButton: View {
    let style: ThemeButtonStyle
    var body: some View {
        Button {
            print("Touched")
        } label: {
            Text("Touch Me")
        }.buttonStyle(style)
    }
}

struct ThemeButtonStyle_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            AppThemeButton(style: .filled)
            AppThemeButton(style: .bordered)
        }
    }
}

Result

enter image description here

时光磨忆 2025-01-25 13:27:30

或者更短

.background(content: {
    switch self.type {
    case .info:
        Color.blue
    case .error:
        Color.red
    case .warning:
        Color.yellow
    }
})

Or Even Shorter

.background(content: {
    switch self.type {
    case .info:
        Color.blue
    case .error:
        Color.red
    case .warning:
        Color.yellow
    }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文