How to create a parent/child hierarchy in Swift

Newbie programmer here! So the app I’m building has a class of repair items and a repair item bank containing the information. A repair item can have children repair items, but not all do. So I've looked into using a tree data structure within a class in order to do this.

But it seems like have mostly empty nodes is inefficient. I'd like to present further repair items if a parent repair item is selected by the user. Is there a better way of handling this?

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

Not sure I understand your question, but is this what you are looking for? Having your children array as optional lets you not consume space with empty arrays and only instantiate them if children objects need to be added.

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

Creating Parent Child Hierarchy on Board, You can use SwiftKanban to define a parent-child hierarchy among cards. Parent​-child hierarchies in Kanban are constructed from a single parent card having  In the Parent-child Links view of the card, click the Add Child Card icon on the card. Select the board so that the card will be created on the same or any other board, as required. Select the Card Type by clicking the corresponding card type color. The card type colors will appear only for those that are defined as Child Card Type in the settings.

I want to say THANK YOU to all that replied. I apologize for phrasing my question more clearly. Essentially, what I needed what a way to relate two of the same type of objects. In other words, I wanted to show linked objects (of same type) if a parent object was selected by the user. I got it to work by storing an array of children item objects. I'm posting my code in case anyone in the future runs into a similar circumstance.

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

    }

}

Defining Hierarchy Between Card Types, SwiftKanban allows defining of a parent-child relationship between different cards to The most significant thing about Card Hierarchy in SwiftKanban is the based on which the team members can implement the Card Hierarchy feature on  In the Dimension Settings panel, select Create Hierarchy Parent-Child Hierarchy. If other parent-child hierarchies already exist, select beside Hierarchies in the Dimension Settings panel. Type a name for the parent of the hierarchy, type a description (optional), and then select Done .

"So the app I’m building has a class of repair items and a repair item bank containing the information. A repair item can have children repair items, but not all do." Not sure if i understand this enough in order to to tell which one is the superclass.

You should subclass. Set your repairItem as your superclass and then for the items with extra properties you subclass them

 class repairItemsWithExtraProperties: repairItems{ }

check the documentation for detailed explanation: https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html

Accessing and Modifying the Node Tree, You create the node tree by creating parent-child relationships between nodes. you use the properties in the following table to uncover the tree's structure. The wizard automatically detected the parent-child relationship. In the Properties window, click the ellipsis button () in the NamingTemplate property cell. In the Level Naming Template dialog box, you define the level naming template that determines the level names in the parent-child hierarchy that are displayed to users as they browse cubes.

"A repair item can have children repair items, but not all do." If you are referring to:

var repairItemChildren: [String] = [String]()

don't store it as an optional. if the array is empty, you don't have repair item children, else you do

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

Swift Algorithm Club: Swift Tree Data Structure, Learn how to implement a Swift Tree data structure through this Children are the nodes below a given node; the parent is the node above. Right-click a dimension table that is not associated with any dimension and select Create Logical Dimension, then select Dimension with Parent-Child Hierarchy. In the Logical Dimension dialog, in the General tab, type a name for the dimension. Click Browse beside the Member Key field.

Hierarchies: Finding Parents, Children and Descendents using Swift, In general, excessive use of NotificationCenter ends up creating a difficult to maintain app where it is not entirely clear what is responding to what  Create hierarchical class structure , loop through data table and then add parent and child and return object level is O for Parent, 1 for Child 1 , 2 for ChildA

Finding The First Common Superview In Swift, One of the most basic building blocks of any iOS app is the UIKit class UIView. At the most basic level, the view hierarchy encompasses the parent-child  Create Parent Child Dimension using Microsoft Aanalysis Service Create new level in hierarchy using calculated member Parent child hierarchy in SSRS with Sql Server Data Source

Child View Controllers, UIView to form a hierarchy, a view controller can become the child of another That enables us to compose our UI from multiple building blocks, which willMove(toParent: nil) // Then, remove the child from its parent child. Using Oracle BI Administration tool: This Video shows how to create logical dimensions with Parent-Child Hierarchy. This is a hierarchy that have all members of the same type.

Comments
  • Thanks! I understand how I’d check if there are children, but how would I actually retrieve those children repair items? Should I store repair item objects in the array?
  • Yes, instead of storing String type, store RepairItem (or whatever your object is)
  • "if the array is empty, you have repair item children, else you don't" - that's backwards.