新手程序员来了!因此,我正在构建的应用程序有一个维修项目类和一个包含信息的维修项目库。维修物料可以有下级维修物料,但不是所有都可以。因此,为了做到这一点,我研究了在类中使用树数据结构。
但似乎大部分节点都是空的,效率很低。如果用户选择了父维修项目,我想要显示进一步的维修项目。有没有更好的方法来处理这件事?
class PropertyItem {
let itemDepartment : String
let repairPrice : Float
let repairItem : String
let itemCategory : String
let multiplyByArea : Bool
var repairItemChildren = [String?]()
init(itemDepartment : String, itemCategory : String, repairPrice : Float, repairItem : String, multiplyByArea : Bool, repairItemChildren : [String?]) {
self.itemDepartment = itemDepartment
self.itemCategory = itemCategory
self.repairPrice = repairPrice
self.repairItem = repairItem
self.multiplyByArea = multiplyByArea
self.repairItemChildren = repairItemChildren
}
}
class PropertyItemBank {
var departmentArray = [PropertyItem]()
init() {
departmentArray.append(PropertyItem(itemDepartment: "Exterior", itemCategory: "Foundation", repairPrice: 2.00, repairItem: "Siding", multiplyByArea: true, repairItemChildren: [nil]))
departmentArray.append(PropertyItem(itemDepartment: "Exterior", itemCategory: "Foundation", repairPrice: 2.00, repairItem: "Siding", multiplyByArea: true, repairItemChildren: ["Bricks", "Wood"]))
}
}
发布于 2018-06-13 08:03:24
“因此,我正在构建的应用程序有一个维修项目类和一个包含信息的维修项目库。一个维修项目可以有子维修项目,但不是所有都有。”我不确定我是否足够了解这一点,以便辨别哪个是超类。
你应该子类化。将您的repairItem设置为您的超类,然后对于具有额外属性的项,您将继承它们
class repairItemsWithExtraProperties: repairItems{ }
有关详细说明,请查看文档:https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html
发布于 2018-06-14 01:16:59
我不太明白你的问题,但这就是你要找的吗?让您的子数组作为可选,使您不会消耗空数组的空间,并且仅在需要添加子对象时才实例化它们。
class PropertyItem {
let itemDepartment : String
let repairPrice : Float
let repairItem : RepairItem
let itemCategory : String
let multiplyByArea : Bool
}
class RepairItem {
var title : String?
var children : [RepairItem]?
}
发布于 2018-06-14 03:16:28
我想对所有回复的人说声谢谢。我为我的问题表达得更清楚而道歉。本质上,我需要的是一种将两个相同类型的对象联系起来的方法。换句话说,如果用户选择了父对象,我希望显示链接的对象(类型相同)。我通过存储子item对象的数组来使其工作。我发布了我的代码,以防将来任何人遇到类似的情况。
class PropertyItem {
let itemDepartment : String
let repairPrice : Float
let repairItem : String
let itemCategory : String
let multiplyByArea : Bool
var repairItemChildren : Array = [PropertyItem?]()
init(itemDepartment : String, itemCategory : String, repairPrice : Float, repairItem : String, multiplyByArea : Bool, repairItemChildren : [PropertyItem?]) {
self.itemDepartment = itemDepartment
self.itemCategory = itemCategory
self.repairPrice = repairPrice
self.repairItem = repairItem
self.multiplyByArea = multiplyByArea
self.repairItemChildren = repairItemChildren
}
}
class PropertyItemBank {
var departmentArray = [PropertyItem]()
init() {
departmentArray.append(PropertyItem(itemDepartment: "Exterior", itemCategory: "Foundation", repairPrice: 2.00, repairItem: "Siding", multiplyByArea: true, repairItemChildren: [nil]))
departmentArray.append(PropertyItem(itemDepartment: "Exterior", itemCategory: "Foundation", repairPrice: 2.00, repairItem: "Siding", multiplyByArea: true, repairItemChildren: [PropertyItem(itemDepartment: "Exterior", itemCategory: "Foundation", repairPrice: 2.00, repairItem: "Siding Child", multiplyByArea: true, repairItemChildren: [nil])]))
}
}
https://stackoverflow.com/questions/50826646
复制相似问题