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

Day 59: SortDescriptor problem

Forums > 100 Days of SwiftUI

Hello,

On day 59, challenge 3, we are asked to " Make FilteredList accept an array of SortDescriptor objects to get used in its fetch request."

I could not compile it by using [SortDescriptor].

FilteredList.swift

import CoreData
import SwiftUI

struct FilteredList<T: NSManagedObject, Content: View>: View {
    var fetchRequest: FetchRequest<T>
    var singers: FetchedResults<T> { fetchRequest.wrappedValue }

    enum PredicateType: String {
        case beginsWith = "BEGINSWITH"
        case contains = "CONTAINS"
        case containsCI = "CONTAINS[c]"
    }
    var predicate = PredicateType.beginsWith

    let content: (T) -> Content

    var body: some View {
        List(fetchRequest.wrappedValue, id: \.self) { singer in
            self.content(singer)
        }
    }

    init(nsType: Int, sortType: [SortDescriptor],  filterKey: String, filterValue: String, @ViewBuilder content: @escaping (T) -> Content) {

        if nsType == 0 {
            predicate = .beginsWith
        } else if nsType == 1 {
            predicate = .contains
        } else {
            predicate = .containsCI
        }

        fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortType, predicate: NSPredicate(format: "%K \(predicate.rawValue) %@", filterKey, filterValue))
        self.content = content
    }
}

In ContentView, I call FilteredList as below;

FilteredList(nsType: 0, sortType: [
                SortDescriptor(\.lastName, order: .reverse)
              ], filterKey: "lastName", filterValue: lastNameFilter) { (singer: Singer) in
                Text("\(singer.wrappedFirstName) \(singer.wrappedLastName)")
            }

2      

Hi,

This is also puzzling for me. We can pass [NSSortDescriptor] instead, but that type doesn't have generic equivalent.

*Actually there are constructors for FetchRequest (both regular and generic), which take SortDescrtiptors type, but they are versions without entity parameter: https://developer.apple.com/documentation/swiftui/fetchrequest

2      

I suspect the problem here is in this line

fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortType, predicate: NSPredicate(format: "%K \(predicate.rawValue) %@", filterKey, filterValue))

you have to initialize the underlying value not the wrapped one:

_fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortType, predicate: NSPredicate(format: "%K \(predicate.rawValue) %@", filterKey, filterValue))

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 your entire paywall view 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.