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

SOLVED: Day 59 Challenge

Forums > SwiftUI


I have almost completed the challenge on day 59, but I am trying to figure out part 3 of the challenge where we are supposed to allow our own view FilteredList to accept an array of SortDescriptor.

Apparently [SortDescriptor] is not a valid type. Obviously I am not understanding something, but I can not figure out what I am missing. Can anybody give me a pointer on how to proceed?


import CoreData
import SwiftUI

struct FilteredList<T: NSManagedObject, Content: View>: View {

    @FetchRequest var fetchRequest: FetchedResults<T>
    let content: (T) -> Content

    var body: some View {
        List(fetchRequest, id: \.self) { item in

    init(filterKey: String, filterValue: String, predicate: MyPredicate, sortDescriptors: [SortDescriptor], @ViewBuilder content: @escaping (T) -> Content) {
        var stringPredicate: String

        switch predicate {
        case .beginsWith:
            stringPredicate = "BEGINSWITH"
        case .contains:
            stringPredicate = "CONTAINS"
        case .predIN:
            stringPredicate = "IN"

        _fetchRequest = FetchRequest<T>(sortDescriptors: sortDescriptors, predicate: NSPredicate(format: "%K \(stringPredicate) %@", filterKey, filterValue))
        self.content = content
enum MyPredicate {
    case beginsWith, contains, predIN


import CoreData
import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @State private var lastNameFilter = "A"

    var body: some View {
        VStack {
            // list of matching singers
            FilteredList(filterKey: "lastName",
                         filterValue: lastNameFilter,
                         predicate: .beginsWith,
                         sortDescriptors: [
                            SortDescriptor(\.wrappedFirstName, order: .reverse)
                         ]) { (singer: Singer) in
                Text("\(singer.wrappedFirstName) \(singer.wrappedLastName)")


Hi @Moghi! SortDescriptor should be of this type.

 @State private var sortDescriptors = [SortDescriptor<Singer>]()

if not mistaken by changing the type parameter should fix the problem.

init(filterKey: String, filterValue: String, predicate: MyPredicate, sortDescriptors: [SortDescriptor<T>], @ViewBuilder content: @escaping (T) -> Content)




thanks for your input, I think it gave me a better understanding regarding the typing. I went and put the following line in my ContentView:

@State private var singerSortDescriptors: [SortDescriptor<Singer>] = [SortDescriptor(\.wrappedLastName),
                                                                       SortDescriptor(\.wrappedFirstName, order: .reverse)]

This state variable was then used in the FilteredList(...).

I also changed the initializer of FilteredList :

init(filterKey: String,
      filterValue: String,
      predicate: MyPredicate,
      sortDescriptors: [SortDescriptor<T>],
      @ViewBuilder content: @escaping (T) -> Content) {...}

unfortunately I get the following error at runtime:

Thread 1: Fatal error: Singer must be introspectable by the objective-c runtime in order to use it as the base type of a `SortDescriptor`.



you are using wrong paths:

  SortDescriptor(\.wrappedFirstName, order: .reverse)]

those are used to display strings on views, on CoreData properties we changed it to this, right?

    var wrappedFirstName: String {
        firstName ?? "Unknown"

    var wrappedLastName: String {
        lastName ?? "Unknown"

But to access items in CoreData Entity we need to use real paths so you need to use it as follows:

  SortDescriptor(\.firstName, order: .reverse)]



yes, you are absolutley right. I think i got it now. I changed the properties as you suggested and the problem went away.

Thank you so much for your help.


Nice! Glad it helped :)


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.