如何在Swift中创建父/子层次结构

时间:2018-06-12 22:23:56

标签: swift

新手程序员在这里!所以我正在构建的应用程序有一类修复项目和一个包含该信息的修复项目库。修理项目可以让孩子修理物品,但不是全部都可以。所以我考虑在类中使用树数据结构来执行此操作。

但似乎大多数空节点效率低下。如果用户选择了父修复项目,我想提供进一步的修复项目。有没有更好的方法来处理这个?

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

4 个答案:

答案 0 :(得分:0)

“所以我正在建造的应用程序有一类维修项目和一个包含这些信息的维修项目库。维修项目可以让孩子修理物品,但并非所有人都这样做。”不知道我是否理解这一点,以便告诉哪一个是超类。

你应该是子类。将repairItem设置为超类,然后将具有额外属性的项设置为子类

 class repairItemsWithExtraProperties: repairItems{ }

查看文档以获取详细说明:https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html

答案 1 :(得分:0)

不确定我理解你的问题,但这是你在找什么?将子数组作为可选项使您不会占用空数组的空间,只有在需要添加子对象时才实例化它们。

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]?
}

答案 2 :(得分:0)

我想对所有回复的人说谢谢。我为更清楚地表达我的问题而道歉。基本上,我需要什么方法来关联两个相同类型的对象。换句话说,如果用户选择了父对象,我想显示链接对象(相同类型)。我通过存储子项目对象的数组来实现它。我发布了我的代码,以防将来有人遇到类似的情况。

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

    }

}

答案 3 :(得分:-1)

“维修项目可以让孩子修理物品,但不是全部都可以。” 如果你指的是:

var repairItemChildren: [String] = [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
    if let children = repairItemChildren {
        self.repairItemChildren = children
    }
}