0

I am trying to make a grid view in SwiftUI, composed of a varying amount of view elements. I used this thread as an orientation: UICollectionView and SwiftUI?

I have an collection that holds my elements, chunked into snippets of 3 each:

let test = allTheRewards.filter({ !$0.completed }).chunked(into: 3)

Then, in my "mother view", I'm iterating through them like this to pass the data to the child view element:

VStack {

                            ForEach(self.test.indices, id:\.self) { idx in

                                HStack {

                                    ForEach(self.test[idx].indices, id:\.self) { index in

                                        HStack {


                                            TestReward(name: self.test[idx][index].name, description: self.test[idx][index].description, shape: self.test[idx][index].shape, bgStart: self.test[idx][index].bgStart, bgEnd: self.test[idx][index].bgEnd, isComplete: self.test[idx][index].completed)


                                        }

                                    }

                                }

                            }

                        }

The problem here is that the compiler doesn't seem to like that many uses of the indexes ([idx][index]).

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

It works fine when I'm only using it a few times in the call to pass my arguments, but it doesn't work with the current 6 times. The compiling time increases a lot and it eventually fails. So I think this may be a pretty inefficient way to go about it. But I don't know what would be a better solution. Any ideas?

Further info: It is no problem to use the amount of indexes in separate elements, like this:

Text("\(self.uncompletedRewards[idx][index].name)")
Text("\(self.uncompletedRewards[idx][index].description)")
Text("\(self.uncompletedRewards[idx][index].shape)")
Text("\(String(describing: (self.uncompletedRewards[idx][index].bgStart)))")
Text("\(String(describing: (self.uncompletedRewards[idx][index].bgEnd)))")
Text("\(String(self.uncompletedRewards[idx][index].completed))")

I just seem not able to put this all into one call...

1 Answer 1

1
TestReward(model: model, row: row, column: column)

or

TestReward(row: row, column: column).environmentObject(model)

should solve your trouble and dramatically increase readability (and next maintenance) of your codebase.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I actually created a helper function to pass the data to the view. It also works and is quite fast. But I don't know if it should be done like that....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.