我有5个这样的数据库设置,例如Parent-> Child-> Grandchild-> Grand-Grandchild-> Grand-Grand-Grandchild,到目前为止,我可以算到特定父级的3级,但是不能不能更深入到最后两个水平。
class Parent : Object {
@objc dynamic var name : String = ""
let childs = List<Child>()
}
class Child : Object {
@objc dynamic var name : String = ""
let grandchilds = List<Grandchild>()
var giveBirth = LinkingObjects(fromType: Parent.self, property: "childs")
}
class Grandchild : Object {
@objc dynamic var name : String = ""
var giveBirth = LinkingObjects(fromType: Child.self, property: "grandchilds")
}
class Grand-Grandchild : Object {
@objc dynamic var name : String = ""
var giveBirth = LinkingObjects(fromType: Grandchild.self, property: "grand-grandchilds")
}
class Grand-Grand-Grandchild : Object {
@objc dynamic var name : String = ""
var giveBirth = LinkingObjects(fromType: Grand-Grandchild, property: "grand-grand-grandchilds")
}
我用这一行来计数孙子代数
if let parent = itemsFromParentList?[indexPath.row] {
parent.childs.flatMap { $0.grandchilds }.count + parent.childs.count
}
我坚持使用一种解决方案,该解决方案可以计算属于特定父级的所有子项(在需要增长时还可以继承子项数据库)。
非常感谢!