WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.