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

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: https://www.hackingwithswift.com/example-code/language/how-to-use-map-to-transform-an-array

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: https://stackoverflow.com/questions/58574847/how-to-dynamically-create-sections-in-a-swiftui-list-foreach-and-avoid-unable-tI 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.

2      

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($0.name) || 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: { $0.name < $1.name }), dictionaryByStatus["Completed"]?.sorted(by: { $0.name < $1.name })].compactMap({ $0 })
        } else if byNameDateType == 1 {
            // Sort by date
            return [dictionaryByStatus["Scheduled"]?.sorted(by: { $0.date < $1.date }), dictionaryByStatus["Completed"]?.sorted(by: { $0.date < $1.date })].compactMap({ $0 })
        } else {
            // Sort by program type
            return [dictionaryByStatus["Scheduled"]?.sorted(by: { $0.date < $1.date }), dictionaryByStatus["Completed"]?.sorted(by: { $0.type < $1.type })].compactMap({ $0 })
        }
    }

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.