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

SOLVED: Day 59 Challenge

Forums > SwiftUI

@Moghi  

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?

FilteredList

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
            self.content(item)
        }
    }

    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
}

ContentView

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(\.wrappedLastName),
                            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)

   

@Moghi  

Hi,

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`.

thanks

   

you are using wrong paths:

[SortDescriptor(\.wrappedLastName),
  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(\.lastName),
  SortDescriptor(\.firstName, order: .reverse)]

   

@Moghi  

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

   

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, 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!

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.