简体   繁体   English

Swift HStack 视图不符合协议“视图”

[英]Swift HStack View not conforming to protocol 'View'

I have built this code我已经建立了这段代码

struct StarDifficultyView: View {

var numberOfStarsToShow: Int
var numberOfTotalStarsToShow: Int = 5

var body: some View {
    HStack{
        var numberLeftToShow = numberOfStarsToShow
        ForEach(1..<numberOfTotalStarsToShow+1){_ in
            if(numberLeftToShow > 0){
                Image(systemName: "star.fill")
                    .foregroundColor(Color.yellow)
                numberLeftToShow -= 1
            }else{
                Image(systemName: "star.fille")
                    .foregroundColor(Color.yellow)
            }
        }
    }
}

} }

It gives me an error on the line if(numberLeftToShow > 0){ saying "Type '()' cannot conform to 'View'" if(numberLeftToShow > 0){说“类型'()'不能符合'视图'”,它会给我一个错误

Can anyone tell me what I'm doing wrong谁能告诉我我做错了什么

Don't throw away the closure parameter for the ForEach !不要丢弃ForEach的闭包参数!

var body: some View {
    HStack{
        ForEach(0..<numberOfTotalStarsToShow){ i in // don't ignore the "i" here by writing "_"

            // "i" will be different in each "iteration"
            // use that to figure out which image to show
            if(i < numberOfStarsToShow){
                Image(systemName: "star.fill")
                    .foregroundColor(Color.yellow)
            } else {
                Image(systemName: "star")
                    .foregroundColor(Color.yellow)
            }
        }
    }
}

Never mind, I just did this没关系,我就是这么做的

struct StarDifficultyView: View {

    var numberOfStarsToShow: Int
    var numberOfTotalStarsToShow: Int = 5

    var body: some View {
        HStack{
            ForEach(1..<numberOfStarsToShow+1){_ in
                Image(systemName: "star.fill")
                    .foregroundColor(Color.yellow)
            }
            ForEach(1..<numberOfTotalStarsToShow-numberOfStarsToShow+1){_ in
                Image(systemName: "star.fill")
                    .foregroundColor(Color.gray)
                    .opacity(0.7)
            }
        }
    }
}

Basically, it just loops through the number of yellow stars to show and then works out how many grey ones to show and does another ForEach to display the leftover ones needed基本上,它只是循环显示要显示的黄色星星的数量,然后计算出要显示的灰色星星的数量,并执行另一个 ForEach 来显示所需的剩余星星

Explaining the issue:解释问题:

You should not add expressions inside the view builder.您不应该在视图构建器中添加表达式。 So numberLeftToShow -= 1 will throw and error because it returns a void ('aka' type()) and this does not conform to View !所以numberLeftToShow -= 1会抛出错误,因为它返回一个void ('aka' type()) 并且这不符合View that is the exact reason for the compiler!这就是编译器的确切原因!

Note 1注1

Don't use SwiftUI like the UIKit!不要像 UIKit 那样使用 SwiftUI! SwiftUI views may execute over time on any state change and should not be used for calculating anything in this way SwiftUI 视图可能随时间在任何 state 更改上执行,不应用于以这种方式计算任何内容

Note 2笔记2

You can convert 1..<numberOfTotalStarsToShow+1 to a closed range like 1...numberOfTotalStarsToShow您可以将1..<numberOfTotalStarsToShow+1转换为封闭范围,例如1...numberOfTotalStarsToShow

Note 3注3

Try not to use branch and convert your if/else code to something like:尽量不要使用分支并将您的 if/else 代码转换为:

Image(systemName: numberLeftToShow > 0 ? "star.fill" : "star.fille")
    .foregroundColor(Color.yellow)

Note 4:注4:

The lower bound of a range can not be less than the upper range, but you can iterate over a reversed range like:范围的下限不能小于范围的上限,但您可以迭代反向范围,例如:

(1...numberOfTotalStarsToShow).reversed()

Note 5:注5:

try use a single source of truth like the forEach parameter itself!尝试使用像 forEach 参数本身这样的单一事实来源!

Final Result:最后结果:

    var body: some View {
        HStack {
            ForEach((1...numberOfTotalStarsToShow).reversed(), id:\.self) { i in
                Image(systemName:  i > 0 ? "star.fill" : "star.fille")
                    .foregroundColor(Color.yellow)
            }
        }
    }

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

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