繁体   English   中英

要求“moveView”符合 swiftui 中的“StringProtocol”

[英]requires that 'moveView' conform to 'StringProtocol' in swiftui

我做了一个结构来传递 ObservedObject 值的变化。

struct moveView : View {

    @Binding var move : Bool

    var body: some View {
        if move{
           return Text("1")
        }else{
           return Text("2")
        }
    }
}
struct TextView : View {

    @ObservedObject var skc:xiangcene

    var body: some View {
        Text("test")
         .opacity(moveView(move: $skc.move) == "1" ? 0.1 : 0.8)    // it show
Referencing operator function '==' on 'StringProtocol' requires that 'moveView' conform to 'StringProtocol'
    }
}

编译器投诉“在'StringProtocol'上引用运算符function'=='要求'moveView'符合'StringProtocol'”我该如何解决?

您的结构属于“视图”类型,并且您正在将其与“字符串”常量进行比较。

您可以检查 boolean 值:

.opacity(moveView(move: $skc.move).move ? 0.1 : 0.8)

但使用表达式:

moveView(move: $skc.move)

在这里没有意义。 您在这里使用变量的值创建一个新视图。

更好的:

.opacity( skc.move ? 0.1 : 0.8)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM