1

How to add new item to multiple dimension array?

    struct Cat: Codable { var childrens: [Cat]? var name: String? var colour: String? } 

This is my current array

var currentShop = [
            [ 
                "children": [ 
                    [
                        "children": [], 
                        "name": "Tom", 
                        "colour": "Brown"
                    ], 
                    [ 
                        "children": [], 
                        "name": "Anggie", 
                        "colour": "White" 
                    ], 
                    [ 
                        "children": [
                            [ 
                                "children": [], 
                                "name": "Lily", 
                                "colour": "White" 
                            ]
                        ], 
                        "name": "Snowy", 
                        "colour": "White" 
                    ], 
                ], 
                "name": "Sandy", 
                "colour": "Brown" 
            ], 
            [ 
                "children": [], 
                "name": "Mike", 
                "colour": "Black"
            ], 
            [ 
                "children": [], 
                "name": "Tommy", 
                "colour": "White" 
            ], 
            "name": "Blackie",
            "colour": "Black"
        ]

New Array

var newChildren = [
            [
                "children": [], 
                "name": "Pus", 
                "colour": "White" 
            ],
            [
                "children": [], 
                "name": "Boots", 
                "colour": "White"
            ]
]

Adding in the new children for Cat name under Snowy. How do I able to update the cats in the currentShop array?
My current code to find the parent

func updateCatShop(parentName:String, Cats: [Cat], newCats:[Cat] ) -> [Cat] { 
    var catList = Cats for var cat in Cats { 
        if(cat.name == parentName) { 
            var updateCats = cat.childrens! + newCats cat.childrens = updateCats
        } else { 
            for (_, childCat) in cat.childrens!.enumerated(){
                updateCatShop(parentName: parentName, Cats: [childCat], newCats: newCats)
            } 
        } 
    }
    return catList
}

var currentShop = updateCatShop(parentName:"Snowy", Cats:currentShop, newCats: newChildren)

What would be the best way to update a multiple dimension object?

4
  • 3
    Your data model is all over the place and very hard to follow. You should definitely not just be using Dictionaries here. Work on making your data model easier to work with. The reason you are having trouble here is because you haven't spent the time to make the data work for you. Commented Feb 18, 2020 at 9:34
  • 2
    Also... the name Blackie and colour Black don't appear to belong to a Cat? You have Sandy with lots of children (also, children is plurals, don't name it childrens), Mike and Tommy (with no children). But Blackie appears to be incorrect. There is no Cat object associated with them. Commented Feb 18, 2020 at 9:38
  • Hi @Fogmeister, thank you for your feedback. I understand, but I creating an app which the view is similar like Reddit comment and together with pagination. That's why my data model would be like that. There's some paging in the middle to load more of the Cats. I just want to know how to add new items in multiple dimension arrays. Commented Feb 18, 2020 at 9:42
  • No worries :) one of the hardest parts of any software engineering is manipulating the data so that it works for you. You might be able to find a way to append to an array deep within a recursive data structure. But... it might be easier to change your data structure to make it flat and then it will be trivial to append data to it but still provide the same insight of the data itself. Commented Feb 18, 2020 at 10:51

1 Answer 1

1

This is not a complete answer but a suggestion of a direction to go in order to improve your data model and make it easier to work with.

Currently you are using recursive Structs and that is making it difficult to work with. But... you still need to keep the relationship between parents and children.

I am presuming too that your Shop will keep track only of cats (and parents and children) who are in stock in the shop.

An initial improvement of your data model might be something like this...

struct Cat {
    let id: String
    let name: String
    let colour: String
    var children: [String] // this is now an array of IDs
}

Now you can maintain a one dimensional array of Cats... or... even better... a Shop.

struct Shop {
    var cats: [String: Cat]

    func add(cat: Cat, parentID: String?) {
        cats[cat.id] = cat

        if let parentID = parentID,
           let parentCat = cats[parentID] {
            parentCat.children.append(cat.id)
        }
    }
}

Or something along the lines of this. This will need tweaking as it may not update the array of children properly.

You would also have to make sure that the cat IDs are unique.

The next iteration of this would be to store your data either in CoreData or in some other storage like Firestore etc... Where you can much more easily store lots of cats in lots of shops and having a nice data model becomes more important.

Sign up to request clarification or add additional context in comments.

3 Comments

This is a very good solution for immutable data structures, I use it myself to represent trees as a flat list. However, note that you need to keep also parentId in every Cat, otherwise you won't be able to find the "root" cats easily. Also, it will get much easier if the array of cats is not an actual array but a dictionary [String: Cat]. Therefore you can access cat by its id fast.
@Sulthan yeah, it depends if there is a requirement to keep track of the root? From the question and the data it looks like this is just a shop inventory where the inventory also happens to have a one to many relationship with itself. The side effect of that is that it forms a tree but I don't think that is a fundamental requirement of the data? Or I might be wrong 🤷🏻‍♂️🐈
@Sulthan I definitely like the idea of using a dictionary of cats like this with id -> Cat pairs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.