UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How do I fix error when trying to use chunking algorithm

Forums > SwiftUI

I wanted to use the .chunked function described in the article about Algorithms.
here is the code.

    @Environment(\.managedObjectContext) private var viewContext
    @Binding var tabSelection: Int
    @FetchRequest(entity: Match.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Match.date, ascending: false)], animation: .default)
    var matches: FetchedResults<Match> 
    //  var key = "Show"
    var match = [Match]()
    var score = [Score]()
    var scoresByBoard = [Score]()
    var boardSet = [Score]() // subset of boards
    @State var suitx = ["♣️", "♦️", "♥️", "♠️", "🚫"] 
    var body: some View {
                List {
                    ForEach (matches, id: \.self) { match in
                        Section(header: Text(match.wrappedClub).headerStyle())
                            {
                            let tt = Int(match.maxPlayers)/4
                           let boardSets = match.scoreArray.chunks(ofCount: tt)
                          ForEach (boardSets, id: \.self) { score in
                        //    for score in boardSets {
                                HStack {
                                    Text(score.wrappedBoardNo)
                                    Text(score.bidLevel ?? "?")
                                    Text(suitx[Int(score.bidSuit ?? "0") ?? 0])
                                    Text(score.bidBy ?? "?")
                                    Text(score.bidMade ?? "?")
                                    Text(score.bidDown ?? "?")
                                    Text(score.nsScore.description)
                                    Text(score.ewScore.description)
                                    }
                                }
                        }  
                    }  
                    }.listStyle(.plain)// End List
                    /*  .toolbar {
                    EditButton()
                    Button(action: addMatch) {
                    Label("Add Item", systemImage: "plus")
                    }
                }  
            }  
        }

If I remove the / / around the additional Text fields, the errors generated will disappear, but when I try to build, I get an error at the view level that says the compiler can't check the code in a reasonable time, and try to break it up.

Not sure what to try next. Would appreciate any suggestions

1      

In that case those are two different match variables.

var match = [Match]() is an array of Match items.

ForEach (matches, id: \.self) { match in <-- This match is the internal variable for the closure of the ForEach. It is a single item from the matches fetched results array. It is not an array itself.

There is no problem having these two match variables... except that, from the given code, the first one is completely unnecessary, as nothing is ever done with it.

1      

Anyway, the problem here is that chunks returns basically an array of arrays so you can't use it in a ForEach. I'm not really sure exactly what it is you're trying to do but a different approach will be required.

1      

roosterboy - OK, that makes sense.
Obelix - thanks for your help. I get the point on the two match variables. tt is total tables - holdover from use in my first app which I am rewriting to use CoreData and SwiftUI.

Apologize for not getting the formating right, but am not sure what I need to do to make that happen. If there is more detailed info on exactly how to do it, I will definitely do in the future. I thought I only needed to put the code between the backticks.

Appreciate the responses. I can accomplish what I need ot do another way, but if it had worked, would have been efficient approach.

1      

I thought I only needed to put the code between the backticks.

That's all you need to do. IIRC, the problem with your initial post was that you used straight quotes ''' instead of backticks ```.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.