NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: SectionedFetchRequest: how to get the count of items in each section?

Forums > SwiftUI

Greetings,

I've managed to get the SectionedFetchRequest to work as I want to, however, I've spent a lot of time trying to display the count of items in each Sections.

I've used the simple section.count and while it does not throw an error, it does not build either.

Here is some bits of code for context:

@SectionedFetchRequest(
        sectionIdentifier: \MeasureData.month,
        sortDescriptors: [SortDescriptor(\MeasureData.measureDate, order: .reverse)]
)
public var sectionedMeasures: SectionedFetchResults<String, MeasureData>
List {
        ForEach(sectionedMeasures) { section in
                Section(header:
                Text(section.id)
                + Text(" (")
                //+ Text(section.count)
                + Text(")")
                ){
                        ForEach(section) { measure in
                        .........
                        }
                }
        }
}

As soon as I uncomment the line + Text(section.count), nothing builds anymore.

Any tips would be appreciated :)

Thank you

   

count is an Int but you are feeding it into a Text, which expects a String.

There is no reason to concatenate Text elements like you are doing. Just do this instead:

Section(header: Text("\(section.id) (\(section.count))")) {
    ForEach(section) { measure in
        ...
    }
}

   

Hi @roosterboy. Pretty sure I tried that and duiwork either. Let me try again and report back. Thank you

   

Don't know what I did wrong then yesterday, but it worked perfectly this time. Thank you very much!

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.