如何在SwiftUI中的ForEach循环中增加状态? [英] How to increment a state in a ForEach loop in SwiftUI?

查看:205
本文介绍了如何在SwiftUI中的ForEach循环中增加状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要增加 genNum 变量并将其传递给ForEach循环中的另一个结构.我的代码正在正确编译,但无法在模拟以及画布中预览.得到无法在该文件中预览".我收到的另一个错误是"RemoteHumanReadableError:操作无法完成. (BSServiceConnectionErrorDomain错误3.)".

I need to increment the genNum variable and pass it to another struct in a ForEach loop. My code is compiling properly but cannot preview it in the simulation as well as the canvas. Getting "cannot preview in this file". Another error I am getting is "RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)".

BSServiceConnectionErrorDomain(3): == BSErrorCodeDescription:OperationFailed

BSServiceConnectionErrorDomain (3): ==BSErrorCodeDescription: OperationFailed

import SwiftUI

@available(iOS 14.0, *)
struct CategoryView: View {
    @State var genNum: Int = -1
    
    var categories: [Int: [PokemonData]] {
        Dictionary(
            grouping: pokemonData,
            by: { $0.generation }
        )
    }
    
    var columns: [GridItem] = [
        GridItem(.fixed(170)),
        GridItem(.fixed(170))
    ]

    var body: some View {
        NavigationView {
            ScrollView {
                LazyVGrid(columns: columns) {
                    ForEach(categories.keys.sorted(), id: \.self) { key in
                        CategoryCard(genNum: self.increment()) // <--- Having problem with this line
                    }
                }
                AllCard()
                    .padding(.horizontal, 15)
                    .padding(.bottom, 20)
            }
            .navigationTitle("Categories")
        }
    }
    
    // Function to increment the state value
    func increment() -> Int {
        self.genNum += 1
        let i = genNum
        return i
    }
}

推荐答案

如果您要做的只是将0到count-1的值传递给CategoryCard()初始化程序,则可以使用enumerated()函数.该函数将一个数组作为输入并返回一个元组数组,其中第一项是数组索引,第二项是原始数组中的元素.

If all you want to do is to pass values from 0 to count-1 to your CategoryCard() initializer, you can use the enumerated() function. That function takes an array as input and returns an array of tuples where the first item is an array index and the 2nd item is the the element from the original array.

此代码,例如:

let array = ["zero", "one", "two", "three"]

array.enumerated().forEach { (index, string) in
    print (index, string)
}

输出以下内容:

0 zero
1 one
2 two
3 three

所以您可以这样重写代码:

So you could rewrite your code like this:

            LazyVGrid(columns: columns) {
                
                ForEach(categories.keys.sorted().enumerated(), id: \.self) { (index, key) in
                    CategoryCard(genNum: index) // <--- Having problem with this line
                }
            }

(免责声明:我还没有使用SwiftUI,所以我对LazyGrid内的ForEach所做的事情还不清楚)

(Disclaimer: I haven't used SwiftUI yet, so I'm not totally clear on what ForEach is doing inside your LazyGrid)

这篇关于如何在SwiftUI中的ForEach循环中增加状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆