核心数据,嵌套列表
Posted
技术标签:
【中文标题】核心数据,嵌套列表【英文标题】:Core Data, Nested List 【发布时间】:2021-01-21 04:53:51 【问题描述】:我尝试在嵌套列表中使用从 Core Data 获取的数据,但是当我添加“children: .listofTasksArray”时,会显示以下错误: “关键路径值类型'[ListOfTasks]?'无法转换为上下文类型“FetchedResults?” 这是核心数据文件
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
这是我用来获取数据并在列表中使用它的列表代码,尝试使用 ListOfTasks 属性“listofTasksArray”作为列表的子项来创建嵌套列表。
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) )
【问题讨论】:
【参考方案1】:数据容器的类型应该是相同的,所以尝试将FetchedResults
包装到一个数组中,例如(可能需要对选项进行一些调整)
var body: some View
List(Array(lists), children: \.listofTasksArray)
Text($0.wrappedTitle
【讨论】:
谢谢你,它的工作知道,有没有办法将“FetchedResults”转换为“数组”所以我可以在“SwiftUI”中将它作为常规数组处理以上是关于核心数据,嵌套列表的主要内容,如果未能解决你的问题,请参考以下文章