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

SOLVED: Remap array to get an sectioned list

Forums > SwiftUI

I have been having problems with my sectionedFetchRequests on my CoreData app, so I'm now trying to reconstruct them not using this option, but "remaping" data array for my lists myself, using .map option as explained by Paul:

But for my sectioned list purposes, I need to rebuild my "Rehearsals" array into another "SectionedRehearsals" one, this way:

struct SectionedRehearsals: identifiable {
   let id: UUID()
   let title: String
   let rehearsals: [Rehearsal]

All of my "Rehearsals" have an "month" property, wich is the one I need data to be grouped by. But event studying this post: don't find the correct sintax to .map the original array to this one, in order to later create a nested ForEach to build this sectioned list.

Any help about this, please? Thanks in advance.


Without seeing more of your code its hard to give help. However, in my app I sectioned out my programs based on statusText field, which is either "Completed" or "Scheduled". Here is the code I used whiched uses a Dictionary to group on. I hope this will help ya.

func sectionedProgramData(searchTerm: String, byNameDateType: Int) -> [[Program]] {
        var filteredPrograms: [Program]

        // Filter the list of programs
        filteredPrograms = programs.filter {
            searchTerm.appearsIn($ || searchTerm.appearsIn($0.monthYear)

        // Create a sectioned array of programs
        let dictionaryByStatus = Dictionary(grouping: filteredPrograms, by: { $0.statusText })
        if byNameDateType == 0 {
            // Sort by name
            return [dictionaryByStatus["Scheduled"]?.sorted(by: { $ < $ }), dictionaryByStatus["Completed"]?.sorted(by: { $ < $ })].compactMap({ $0 })
        } else if byNameDateType == 1 {
            // Sort by date
            return [dictionaryByStatus["Scheduled"]?.sorted(by: { $ < $ }), dictionaryByStatus["Completed"]?.sorted(by: { $ < $ })].compactMap({ $0 })
        } else {
            // Sort by program type
            return [dictionaryByStatus["Scheduled"]?.sorted(by: { $ < $ }), dictionaryByStatus["Completed"]?.sorted(by: { $0.type < $1.type })].compactMap({ $0 })


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 October 1st.

Click to save your free spot 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.