GO FURTHER, FASTER: Try the Swift Career Accelerator 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: 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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

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.