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

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)")
            }

   

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

   

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))

   

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.