WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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.

   

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.

   

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.

   

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 ```.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Reply to this topic…

You need to create an account or log in to reply.

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.