Reputation: 874
I'm try to use my data that fetched from Core Data in nested list, but when I add the "children: .listofTasksArray" the following error is shown: "Key path value type '[ListOfTasks]?' cannot be converted to contextual type 'FetchedResults?'" this is the core data file
extension ListOfTasks {
@nonobjc public class func fetchRequest() -> NSFetchRequest<ListOfTasks> {
return NSFetchRequest<ListOfTasks>(entityName: "ListOfTasks")
}
@NSManaged public var addedDate: Date?
@NSManaged public var color: String?
@NSManaged public var favoriteIndex: Int16
@NSManaged public var icon: String?
@NSManaged public var id: UUID?
@NSManaged public var index: Int16
@NSManaged public var isArchived: Bool
@NSManaged public var isFavorite: Bool
@NSManaged public var isLocked: Bool
@NSManaged public var title: String?
@NSManaged public var isList: Bool
@NSManaged public var origin: ListOfTasks?
@NSManaged public var subLists: NSSet?
// The wrapped items
public var wrappedAddedDate: Date {
return addedDate ?? Date()
}
// To convert the color from "String" to "Color" type
public var wrappedColor: Color {
return Color(colorName: color ?? "blue")
}
public var wrappedIcon: String {
return icon ?? "ellipsis.circle.fill"
}
public var wrappedId: UUID {
return id ?? UUID()
}
public var wrappedTitle: String {
return title ?? "Unknown Title"
}
public var listofTasksArray: [ListOfTasks]? {
let set = subLists as? Set<ListOfTasks> ?? nil
return set?.sorted { // sort by index
$0.index > $1.index
}
}
}
and this is the list code that I used to fetch the data and use it in the list trying to make nested list using ListOfTasks property "listofTasksArray" as a child for the list.
struct ListsView: View {
@FetchRequest(entity: ListOfTasks.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \ListOfTasks.index, ascending: true)], animation: .default) private var lists: FetchedResults<ListOfTasks>
var body: some View {
List(lists, children: \.listofTasksArray, rowContent: { Text($0.wrappedTitle) })
}
}
Upvotes: 1
Views: 461
Reputation: 257493
Types of data container are expected to be the same, so try to wrap FetchedResults
into an array, like (might be some tuning needed do to optionals)
var body: some View {
List(Array(lists), children: \.listofTasksArray) {
Text($0.wrappedTitle
}
}
Upvotes: 1