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

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

2      

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)

2      

@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

2      

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

2      

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

2      

Nice! Glad it helped :)

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.